The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
SELECT...WHERE errors using MySQL and PHP3
Discuss SELECT...WHERE errors using MySQL and PHP3 in the MySQL Help forum on Dev Shed. SELECT...WHERE errors using MySQL and PHP3 MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 4th, 1999, 02:58 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
[code]
Here are my lines in the code:
[code]
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 21st, 1999, 06:25 AM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
If you are dealing with strings in MySQL you need quotes. Either use single (') or escaped double (") around the variable you want to use.
eg.
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>$result=mysql_query("SELECT * FROM items WHERE product='$product');[/quote]
The Warning about 0 not being a result index means the SQL statement failed for some reason.
|
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
|
|
|
|
|