MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old December 4th, 1999, 02:58 PM
SepodatiCreations
Guest
Dev Shed Newbie (0 - 499 posts)
 
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


Reply With Quote
  #2  
Old December 21st, 1999, 06:25 AM
rbergs
Guest
Dev Shed Newbie (0 - 499 posts)
 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > SELECT...WHERE errors using MySQL and PHP3


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway