Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-21-2002, 03:13 PM   PM User | #1
wap3
Regular Coder

 
Join Date: Aug 2002
Location: UK
Posts: 324
Thanks: 0
Thanked 0 Times in 0 Posts
wap3 is an unknown quantity at this point
Mysql query

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

PHP Code:
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");

wap3 is offline   Reply With Quote
Old 11-21-2002, 03:29 PM   PM User | #2
Wichetael
New Coder

 
Join Date: Nov 2002
Location: Netherlands
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Wichetael has a little shameless behaviour in the past
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:

PHP Code:
$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

btw, do try to use a more descriptive subject line next time, Thank you.
Wichetael is offline   Reply With Quote
Old 11-21-2002, 04:48 PM   PM User | #3
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Yep,

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

then you can access the fileds this way
$newid = $result['Item_Id'];
bcarl314 is offline   Reply With Quote
Old 11-21-2002, 05:24 PM   PM User | #4
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,224
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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");
}
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:26 PM.


Advertisement
Log in to turn off these ads.