|
|
 |
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
|
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? 
|
|
|
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
|
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..
|
|
|
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
|
Re: Returning a value from a MYSQL Query?
Quote:
Originally Posted by Accurax
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 
|
|
|
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
|
Re: Returning a value from a MYSQL Query?
You still need the fetch_array function tho...
|
|
|
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
|
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..
|
|
|
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
|
Re: Returning a value from a MYSQL Query?
Quote:
Originally Posted by grahame
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 
|
|
|
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
|
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.
|
|
|
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
|
Re: Returning a value from a MYSQL Query?
Quote:
Originally Posted by grahame
$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;
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|