Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > PHP

Reply
 
LinkBack Thread Tools
Old February 27th, 2007, 01:26 PM   #1
New Member
 

Join Date: Feb 2007
Location: Wouldn't you like to know Jacko?
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Kimochi is on a distinguished road
Returning a value from a MYSQL Query?

I am trying to return the highest value for a column in one of my tables so I can insert a new rows with a value one higher.

This is the code I have:
Code:
$sql1 mysql_query("SELECT MAX(chat_id) FROM archive");
    
$maxid $sql1+1
Unfortunately, the variable $maxid is ALWAYS being returned as 6, even when the table archive is empty!

Any suggestions?
Kimochi is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 28th, 2007, 07:38 AM   #2
Reputable Member
 

Join Date: May 2006
Location: Warrington, UK
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Accurax is on a distinguished road
Re: Returning a value from a MYSQL Query?

dont Mysql queries return an array when you do a select from the database ?

Therefore youd need to use:

Code:
$result = mysql_query($sql1)
        or die ("error");
$row = mysql_fetch_array($result);
$maxid = $row['max_id'];
then
Code:
$plusid = $maxid +1;
I could be wrong as ive not actually done exactly what your trying to do before, but this seems more logical to me at least.

Last edited by Accurax; February 28th, 2007 at 07:41 AM..
Accurax is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 28th, 2007, 10:21 AM   #3
New Member
 

Join Date: Feb 2007
Location: Wouldn't you like to know Jacko?
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Kimochi is on a distinguished road
Re: Returning a value from a MYSQL Query?

Quote:
Originally Posted by Accurax View Post
dont Mysql queries return an array when you do a select from the database ?

Therefore youd need to use:

Code:
$result = mysql_query($sql1)
        or die ("error");
$row = mysql_fetch_array($result);
$maxid = $row['max_id'];
then
Code:
$plusid = $maxid +1;
I could be wrong as ive not actually done exactly what your trying to do before, but this seems more logical to me at least.
Yeah true... Not sure if it still applies seeing as I'm just using MAX(). Maybe the MAX() function returns the ROW with the highest value?

I'll give it a try
Kimochi is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 28th, 2007, 10:22 AM   #4
Elite Veteran
 

Join Date: Jan 2007
Location: You know where
Age: 32
Posts: 4,607
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 karinne is a name known to all karinne is a name known to all karinne is a name known to all karinne is a name known to all karinne is a name known to all karinne is a name known to all
Re: Returning a value from a MYSQL Query?

You still need the fetch_array function tho...
karinne is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 28th, 2007, 11:05 AM   #5
Reputable Member
 

Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 grahame is a jewel in the rough grahame is a jewel in the rough grahame is a jewel in the rough
Re: Returning a value from a MYSQL Query?

In other words, try:

$rs = mysql_query("SELECT MAX(chat_id) FROM archive");
$sql1 = mysql_fetch_row($rs);
$maxid = $rs[0]+1;

mysql_query is rather like an fopen - it returns a handle to the resource which is a reult set - in this case just one row with one element in it, which you then read.

Edit - see my later post - the above contains a very silly error on my part - lat line should read $sql1 and not $rs

Last edited by grahame; February 28th, 2007 at 04:10 PM..
grahame is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 28th, 2007, 11:06 AM   #6
New Member
 

Join Date: Feb 2007
Location: Wouldn't you like to know Jacko?
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Kimochi is on a distinguished road
Re: Returning a value from a MYSQL Query?

Quote:
Originally Posted by grahame View Post
In other words, try:

$rs = mysql_query("SELECT MAX(chat_id) FROM archive");
$sql1 = mysql_fetch_row($rs);
$maxid = $rs[0]+1;

mysql_query is rather like an fopen - it returns a handle to the resource which is a reult set - in this case just one row with one element in it, which you then read.
Okay then. Thanks a lot
Kimochi is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 28th, 2007, 12:26 PM   #7
New Member
 

Join Date: Feb 2007
Location: Wouldn't you like to know Jacko?
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Kimochi is on a distinguished road
Re: Returning a value from a MYSQL Query?

Hmm it's still not working :/

The thing is, when I enter the same query in PHP-MA is returns the highest value.
Kimochi is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 28th, 2007, 04:09 PM   #8
Reputable Member
 

Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 grahame is a jewel in the rough grahame is a jewel in the rough grahame is a jewel in the rough
Re: Returning a value from a MYSQL Query?

Quote:
Originally Posted by grahame View Post
$rs = mysql_query("SELECT MAX(chat_id) FROM archive");
$sql1 = mysql_fetch_row($rs);
$maxid = $rs[0]+1;
Silly error on my part - sorry - should have read:

$rs = mysql_query("SELECT MAX(chat_id) FROM archive");
$sql1 = mysql_fetch_row($rs);
$maxid = $sql1[0]+1;
grahame is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks

Tags
max , mysql , query


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Mysql Query not working nate2099 Databases 13 January 2nd, 2008 10:28 PM
MYSQL/PHP Query question andrewlondon Databases 1 September 19th, 2007 07:26 AM
MYSQL PHP query class jhappeal Databases 1 August 16th, 2007 09:52 PM
MySQL query query dangergeek Databases 3 April 12th, 2007 08:45 AM
mysql query in a function Redempt1on PHP 6 May 18th, 2006 09:25 PM


Search Engine Optimization by vBSEO 3.2.0 RC8