Jump to content

Need help putting together a function for highest number


liquinas

Recommended Posts

I'm trying tot get this working. There's 6 records on a table and they have IDs 1 through 6. I need the function to get the highest value of column "ID" and then put this value on variable $lmao.

 

<?php
function getcarinfo($ID)
{
$db = mysql_select_db("tlcmotors")
or die ("Error-Could not select database. Please contact [email protected]");
$query = "SELECT MAX(ID) FROM `cars`";
$result = mysql_query($query)
or die ("Error-Could not execute query. Please contact [email protected]");
return mysql_fetch_array($result,MYSQL_ASSOC);
}
?>
<?php
$lmao="{$carinfo ['ID']}";
echo "$lmao";
?>

change your "or die" statement to

or die("ERROR!<br>".mysql_error())

This will show you whats wrong with your SQL statements.

 

for starters, you don't need to put the column name 'cars' in quotes.

 

<?php
function getcarinfo()
{
$db = mysql_select_db("tlcmotors")
or die ("Error-Could not select database. Please contact [email protected]");
$query = "SELECT MAX(ID) FROM cars";
$result = mysql_query($query)
or die ("Error-Could not execute query. Please contact [email protected]");
return mysql_result($result,0,0);
}

$lmao= getcarinfo();

echo "$lmao";
?>

Thanks. It works. What are the two 0's for in the result line?

 

basically the two zero's tell it to grab the first returned value.

 

in your case, the query statement could only return one value.

 

if your query had requested info from multiple columns, such as:

SELECT MAX(ID), column2 FROM cars

then:

mysql_result($result,0,0); would return the value in column ID

mysql_result($result,1,0); would return the value in the column named column2

 

if your query produced multiple rows,

SELECT ID FROM cars

then

mysql_result($reult,0,0); would return the first ID in your database

mysql_result($reult,0,1); would return the second ID in your database

etc. etc..

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.