/    Sign up×
Community /Pin to ProfileBookmark

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Can anyone tell me what’s wrong with this mysql_fetch_array?

[code=php]$sql = “SELECT * FROM ‘products’ WHERE ‘category’=$ID”;
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))[/code]

In the tutorial it’s written as

[code=php]$sql = “SELECT * FROM PRODUCTS where Category=” . $ID;
$result = @mysql_query($sql);
while ($row=mysql_fetch_array($result))[/code]

But that didn’t work either. The above are the first lines of code after the following…

[code=php]<?php
mysql_connect(“*****.com”, “**********”, “*******”);
mysql_select_db(“*****”);[/code]

I searched for other posts regarding this but I still can’t resolve this. I keep getting “Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/xxxxxxxxxxxxxxxbrowse.php on line 6”

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@DariaDec 20.2005 — What is exactly on the line 6 of your browse.php file?
Copy linkTweet thisAlerts:
@DoppleauthorDec 20.2005 — Line 6 is
[code=php]while ($row = mysql_fetch_array($result))[/code]
This takes $result from line 5 which in turn takes $sql from line 4.

Seems a round about way of doing things but as I'm still learning I thought I'd just follow the tutorial. (I'm regretting it a bit now! ? )
Copy linkTweet thisAlerts:
@NogDogDec 20.2005 — Use "back quotes" to quote table and column names, not regular quotes:
[code=php]
$sql = "SELECT * FROM products WHERE category=$ID";
[/code]

Also, never assume a query is going to work. A simple way to debug:
[code=php]
$result = mysql_query($sql) or die("Query failed: $sql - " . mysql_error());
[/code]

Also, if category is a text value, you will need normal single quotes around '$ID' in the query.

PS: Table and column names are case-sensitive, in case that's an issue here.
Copy linkTweet thisAlerts:
@DariaDec 20.2005 — Try to get an less general error message in order to troubleshoot, you should be able to with this:

[code=php]
if ($row = mysql_fetch_row($res)) {
return $row;
} else {
print (mysql_error());
}
[/code]


[COLOR=red]EDIT: or whatever NogDog just said ? he is better at this![/COLOR]
Copy linkTweet thisAlerts:
@DoppleauthorDec 20.2005 — This is the full code as seen in the tutorial, although the tutorial didn't have the mysql_connect parts in it.
[code=php]<?php
mysql_connect(All the usual stuff here);
mysql_select_db("My SQL database name here");
$sql = "SELECT * FROM 'products' WHERE 'category'=$ID";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
?>
<p align="center"> </p>
<table border="1" width="100%" id="table2" class="ms-color2-main">
<tr>
<td class="ms-color2-tl" width="160">
<img border="0" src='images/<?php echo($row['picurl']) ?>'

width="300" height="300">
</td>
<td class="ms-color2-top" rowspan="2"><?php echo($row['detail'])?>

</td>
<td class="ms-color2-top" width="282" colspan="2">Price: $
<?php echo($row['price'])?>
</td>
</tr>
<tr>
<td class="ms-color2-tl" width="160">
<?php echo($row['description']) ?>
</td>
<td class="ms-color2-top" width="141">Shipping: $
<?php echo($row['weight'] * $ship_factor) ?>
</td>
<td class="ms-color2-top" width="141">
<?php
if ($row['stockcount']<1)
{
?>
<p align="center">OUT OF STOCK</td>
<?php
}
else
{
?>
<p align="center">
<a href='buyit.php?id=
<?php echo($row['id']) ?>'>BUY IT NOW
</a></td>
<?php
}
?>
</tr>
</table>
<?php
}
?>[/code]

I will be able to download the zip file with the source code when I get home but I'd really rather manage myself (to an extent!)
Copy linkTweet thisAlerts:
@DariaDec 20.2005 — Question to NogDog: why backquotes instead of regular?
Copy linkTweet thisAlerts:
@chazzyDec 20.2005 — Dopple - NogDog has already shown you what the problem is, you need to replace the single quotes (') with back-ticks () so it will begin working when you replace<br/>
[code=php]<br/>
$sql = "SELECT * FROM 'products' WHERE 'category'=$ID";<br/>
[/code]<br/>
with<br/>
[code=php]<br/>
$sql = "SELECT * FROM
products WHERE category`=$ID";

[/code]

Question to NogDog: why backquotes instead of regular?[/QUOTE]

mysql uses single quotes and double quotes to denote string text for insertion, and back-ticks for denoting field names (columns, tables, databases, functions, procedures, triggers, etc)
Copy linkTweet thisAlerts:
@DariaDec 20.2005 — never mind ? chazzy answered my Q... thanks
Copy linkTweet thisAlerts:
@NogDogDec 20.2005 — Question to NogDog: why backquotes instead of regular?[/QUOTE]
What Chazzy said:
mysql uses single quotes and double quotes to denote string text for insertion, and back-ticks for denoting field names (columns, tables, databases, functions, procedures, triggers, etc)[/quote]
Copy linkTweet thisAlerts:
@DoppleauthorDec 20.2005 — Thanks for the debugging tips guys.

now I'm getting this "Query failed: SELECT * FROM products WHERE category= - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

So the debugging's working at least.

Could someone actually have a look at the whole code I posted above and tell my if that looks like a complete code for an online store? I've just noticed $ID isn't assigned at all!

Bah!
Copy linkTweet thisAlerts:
@chazzyDec 20.2005 — you have $ID in your code, where do you define it?
Copy linkTweet thisAlerts:
@NogDogDec 20.2005 — It appears $ID is not set. My guess is that the example assumes register_globals is on, which is a very bad assumption. Try adding this line before the query is defined:
[code=php]
if(!empty($_POST['ID']))
{
$ID = $_POST['ID'];
}
else
{
die("ERROR: ID not received from form");
}
[/code]
Copy linkTweet thisAlerts:
@DoppleauthorDec 20.2005 — Guys I appreciate all of your help but I think I'm going to check the source code when I get home. I'll be able to check how bad the tutorial actually is (or alternativly how thick I am for not being able to follow a perfectly good tutorial)

I'll post back once I know either way.

Graham ?
Copy linkTweet thisAlerts:
@DoppleauthorDec 21.2005 — It turns out there's 3 other php files the guy who does the tutorial doesn't tell you about unless you download the zip file with the source code. Unfortunately my work doesn't allow you to download zipz or exes or anything so that was me screwed from the start. Thanks for all of your help though.

Graham
×

Success!

Help @Dopple spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...