liquinas Posted March 1, 2007 Share Posted March 1, 2007 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"; ?> Link to comment https://forums.phpfreaks.com/topic/40757-need-help-putting-together-a-function-for-highest-number/ Share on other sites More sharing options...
alpine Posted March 1, 2007 Share Posted March 1, 2007 SELECT id FROM cars ORDER BY id DESC LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/40757-need-help-putting-together-a-function-for-highest-number/#findComment-197309 Share on other sites More sharing options...
micah1701 Posted March 1, 2007 Share Posted March 1, 2007 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"; ?> Link to comment https://forums.phpfreaks.com/topic/40757-need-help-putting-together-a-function-for-highest-number/#findComment-197310 Share on other sites More sharing options...
liquinas Posted March 1, 2007 Author Share Posted March 1, 2007 Thanks. It works. What are the two 0's for in the result line? Link to comment https://forums.phpfreaks.com/topic/40757-need-help-putting-together-a-function-for-highest-number/#findComment-197317 Share on other sites More sharing options...
micah1701 Posted March 1, 2007 Share Posted March 1, 2007 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.. Link to comment https://forums.phpfreaks.com/topic/40757-need-help-putting-together-a-function-for-highest-number/#findComment-197345 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.