Jump to content

[SOLVED] Querying MySQL database using WHERE condition


kylew00f

Recommended Posts

Hello,

 

I am using MySQL Server version: 4.0.16

 

This is the SQL code in my php script:

 

SELECT * FROM item_details WHERE model=$id

 

I am trying to get information from a row for when the column "model" equals a variable.  The database, table, row, & column do exist.  I assume my WHERE syntax is incorrect since I have been able to display information from rows when using SELECT * FROM tablename before.

 

below is the complete script and the output i get

 

 

 

<?php

include "../php/item_login.php";  //connects to database server & selects database//

if ($connection=1) {
    echo "connected to database.......<br><br>";
}

$id=$_GET['id'];
$sql = ("SELECT * FROM item_details WHERE model=$id");
$result = mysql_query($sql);
$row = mysql_fetch_row($result);


if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print";

}


?> 

<br><br><br>
model#: <? echo $id; ?><br>
height: <? echo $row[2]; ?>

 

 

 

 

http://www.site.com/item.php?item=10-10

 

 

this is the output displayed on the page

 

connected to database.......

No rows found, nothing to print 


model#: 10-10
height: 

 

 

 

this is the output i want

 

 

connected to database.......


model#: 10-10
height: 10

 

 

 

 

any help would be appreciated.  thank you

Try replacing the "SELECT * FROM item_details WHERE model=$id" with "SELECT * FROM item_details WHERE model" and see what happens.

 

It SHOULD select everything. If it doesn't select anything or if it gives an error, then there probably isn't a column called "model" and that's the problem.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.