#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
    [/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

  2. #2
  3. No Profile Picture
    AlexDG
    Guest
    Devshed Newbie (0 - 499 posts)
    I always put

    WHERE col='some'

    istead of

    WHERE col=some

    maybe this is the problem?
  4. #3
  5. No Profile Picture
    rod k
    Guest
    Devshed Newbie (0 - 499 posts)
    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.

IMN logo majestic logo threadwatch logo seochat tools logo