...

Mysql query

wap3
11-21-2002, 03:13 PM
Hi

Can somebody tell me why this wont work. It is supposed to fecth the highest value in the field Item_id.

It wont work though. It just returns 'no' which I put there to see if it wasn't retreiving anything ??

Thanks


function display_add_item_form() {
$conn = db_connect(); //connect to database
$query = mysql_query("SELECT Item_id FROM products ORDER BY Item_id DESC LIMIT 1");
$result = mysql_query($query);
if (!$result)
echo "no";
$newid = $result;
print ("$newid");
}

Wichetael
11-21-2002, 03:29 PM
you're calling mysql_query two times, you should not do that. Instead you should directly assign the return value of the first mysql_query call to $result like so:

$result_=_mysql_query("SELECT Item_id FROM products ORDER BY Item_id DESC LIMIT 1");

Though you will have to make another call to fetch the actual data from the result. with one of the mysql_fetch_* calls, take a look at the php manual here (http://www.php.net/manual/en/ref.mysql.php)

btw, do try to use a more descriptive subject line next time, Thank you.

bcarl314
11-21-2002, 04:48 PM
Yep,

basically change your $resutt line to
$result = mysql_fetch_assoc($query);

then you can access the fileds this way
$newid = $result['Item_Id'];

Spookster
11-21-2002, 05:24 PM
No need to get all fancy:


[php]

function display_add_item_form() {
$conn = db_connect(); //connect to database
$query = "SELECT Item_id FROM products ORDER BY Item_id DESC LIMIT 1";
$result = mysql_query($query);
if (!$result)
echo "no";
$newid = $result;
print ("$newid");
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum