The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
SELECT...WHERE errors using MySQL and PHP3
Discuss SELECT...WHERE errors using MySQL and PHP3 in the PHP Development forum on Dev Shed. SELECT...WHERE errors using MySQL and PHP3 PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 4th, 1999, 03:18 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
Hello All.
I've created a mySQL table called 'items' with the following:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
CREATE TABLE items (id SMALLINT DEFAULT '1' NOT NULL AUTO_INCREMENT, product VARCHAR(50) NOT NULL, manufacturer VARCHAR(50), price DECIMAL(6,2), image TINYTEXT, sizes TINYTEXT, units VARCHAR(15), description TINYTEXT, PRIMARY KEY (id), UNIQUE id (id));
[/quote]
and inserted the following data:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
INSERT INTO items VALUES (1,'truck','Cadillac',15.00,'ls-trucks/cadillac/truck01.gif','5.0,5.25','inches','Available in Raw or Mirror Metallic, Red, and Blue');
[/quote]
The area I'm having trouble with is where I'm pulling information back out of database using PHP. I call a page with '?product=truck' posted onto it. The following SELECT does not return any information when it should return the INSERT I have above.
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
$result = mysql_query("SELECT * FROM items WHERE product=$product",$db);
[/quote]
I get the following error:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
Warning: 0 is not a MySQL result index in C:Inetpubwebpubproducts.php3 on line 26
[/quote]
Here are my lines in the code:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
19: $result = mysql_query("SELECT * FROM items WHERE product=$product",$db);
20: if ($myrow = mysql_fetch_row($result))
[/quote]
if I use the following SELECTs, it pulls the data out correctly.
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
$result = mysql_query("SELECT * FROM items WHERE product='truck'",$db);
$result = mysql_query("SELECT * FROM items WHERE id=1",$db);
[/quote]
and if I post '?id=1' onto the address, the following works:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
$result = mysql_query("SELECT * FROM items WHERE id=$id",$db);
[/quote]
Upon someones suggestion I changed the code in my file to the following:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
18: $statement = ("SELECT * FROM items WHERE product=$product");
19: $result = mysql_query($statement,$db);
20: if ($myrow = mysql_fetch_row($result))
[/quote]
That exact code gives me the same error with the same line number. However, if I change around the SELECT statement to any of the following:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
SELECT * FROM items WHERE product='$product'
SELECT * FROM items WHERE product='truck'
SELECT * FROM items WHERE id=1
[/quote]
I get <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>$result = 2[/quote] but nothing within the IF statement is executed.
Is there something here I'm overlooking. I've read through the manuals, but I couldn't find a solution. I'm new to this, as you can probably tell, and I've been trying to follow a few tutorials to learn how these work. Do I have to define 'product' in a special way so that i can use it in SELECT...WHERE statements? I've tried using HAVING instead of WHERE, but that didn't change anything. Thank you for any help you can provide.
---John Holmes
|

December 4th, 1999, 06:49 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
I always put
WHERE col='some'
istead of
WHERE col=some
maybe this is the problem?
|

December 5th, 1999, 08:21 AM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
Alex is correct.
First, get rid of the if statement.
Change this line:
$result = mysql_query("SELECT * FROM items WHERE product=$product",$db);
to
$result = mysql_query("SELECT * FROM items WHERE product='$product'",$db);
The only time you can exclude the opening and closing quotes around the data is if the data and the field type are both numeric.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|