SN1P3R_85 Posted September 9, 2008 Share Posted September 9, 2008 Hi, i want to run this query, and store the results in a php variable: mysql_query("SELECT MAX(Id) FROM `threads`"); Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/ Share on other sites More sharing options...
dezkit Posted September 9, 2008 Share Posted September 9, 2008 Cool, i want to graduate Harvard and become a millionaire. Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637196 Share on other sites More sharing options...
SN1P3R_85 Posted September 9, 2008 Author Share Posted September 9, 2008 don't say useless stuff in my thread Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637197 Share on other sites More sharing options...
dezkit Posted September 9, 2008 Share Posted September 9, 2008 Sorry, but you just stated something in your original post, nothing needing any help. Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637198 Share on other sites More sharing options...
SN1P3R_85 Posted September 9, 2008 Author Share Posted September 9, 2008 ok, what i meant was, can one of you guys show me how to store that query in a variable. Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637199 Share on other sites More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 function fGetMaxID() { $query = "PREPARE GetMaxID0 FROM 'SELECT max(Id) FROM threads'"; $result = mysql_query($query) or die(mysql_error()); $query = "EXECUTE GetMaxID0"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array ($result, MYSQL_BOTH); $query = "DEALLOCATE PREPARE GetMaxID0"; $resultDeallocate = mysql_query($query) or die(mysql_error()); return $row; } $maxID = fGetMaxID(); $maxID = $maxID[0]; untested, but should work EDIT:: changed query to max(Id) Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637200 Share on other sites More sharing options...
SN1P3R_85 Posted September 9, 2008 Author Share Posted September 9, 2008 All of this just finds the max id in the table and stores it? Is there anything wrong with the query: mysql_query("SELECT MAX(Id) FROM `threads`"); is it possible to store just this query in a variable? Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637202 Share on other sites More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 try: $MaxID = mysql_query("SELECT MAX(Id) FROM `threads`"); $MaxID = mysql_fetch_array($MaxID, MYSQL_BOTH); $MaxID = $MaxID[0]; echo $MaxID; Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637203 Share on other sites More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 basically, max() extracts an array, so you need to use mysql_fetch_array() to get the array, then return the row... it's a single element array, but still an array... so you need to get $MaxID[0] (the result) and set $MaxID to that value... hth Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637205 Share on other sites More sharing options...
SN1P3R_85 Posted September 9, 2008 Author Share Posted September 9, 2008 i dont' understand why this wont' work, this makes sense. I am using my login table to test it. it should echo the first element of the array. <?php $select_maxid = mysql_query("SELECT MAX(Id) FROM `login`"); $row1 = mysql_fetch_array($select_maxid, MYSQL_BOTH); $max_id = $row1[0]; echo $max_id; ?> Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637206 Share on other sites More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 what is it returning? Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637207 Share on other sites More sharing options...
SN1P3R_85 Posted September 9, 2008 Author Share Posted September 9, 2008 nothing at all. btw, what does MYSQL_BOTH do? Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637208 Share on other sites More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 i'm assuming you have a column "Id" in the login table, and that there is at least one record in the table? as far as MYSQL_BOTH, check: http://us2.php.net/mysql_fetch_array technically, it's not needed as it's the default value, but good practice to put it in... EDIT:: Your code works fine for me (short of changing Id to ID, and login to Links to fit my db...) Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637209 Share on other sites More sharing options...
SN1P3R_85 Posted September 9, 2008 Author Share Posted September 9, 2008 hey, thanks for the help, i got it to work. The mysql code you gave me worked fine, it was a weird syntax error. I never found out what it was, my code looked fine, so i copied a piece of code from another file and it worked. Before i leave, could you tell me what that MYSQL_BOTH does? Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637212 Share on other sites More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 "The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works)." from: http://us2.php.net/mysql_fetch_array glad i could help... Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637215 Share on other sites More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 ...and don't forget to mark as topic Solved.... (i always forget...) Link to comment https://forums.phpfreaks.com/topic/123368-mysql-maxid-into-php-variable/#findComment-637220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.