#1
  1. No Profile Picture
    SepodatiCreations
    Guest
    Devshed Newbie (0 - 499 posts)
    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

  2. #2
  3. No Profile Picture
    rbergs
    Guest
    Devshed Newbie (0 - 499 posts)
    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.

Similar Threads

  1. PHP3 + MySQL returns Unknown Error on Connect
    By Projecto EINet in forum PHP Development
    Replies: 2
    Last Post: November 6th, 2003, 04:22 PM
  2. Replies: 1
    Last Post: August 5th, 2003, 08:31 AM
  3. php3 and mySQL
    By faisalkhan in forum MySQL Help
    Replies: 2
    Last Post: July 6th, 2000, 11:41 AM
  4. SELECT...WHERE errors using MySQL and PHP3
    By SepodatiCreations in forum PHP Development
    Replies: 2
    Last Post: December 5th, 1999, 08:21 AM
  5. MySQL + PHP3 gives Unknown Error on Connect
    By Projecto EINet in forum MySQL Help
    Replies: 4
    Last Post: October 27th, 1999, 04:32 AM

IMN logo majestic logo threadwatch logo seochat tools logo