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`"); Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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) Quote Link to comment 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? Quote Link to comment 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; Quote Link to comment 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 Quote Link to comment 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; ?> Quote Link to comment Share on other sites More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 what is it returning? Quote Link to comment 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? Quote Link to comment 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...) Quote Link to comment 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? Quote Link to comment 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... Quote Link to comment 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...) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.