tmyonline Posted November 14, 2007 Share Posted November 14, 2007 Hi guys: Here I have: $SQL = "SELECT element_key, matrix_num FROM matrix"; $Result = mysql_db_query($db,$SQL,$cid); I would like to find the maximum element in the element_key column and that in the matrix_num column. This is what I did but it didn't work. $element = $Result['element_key']; $max_element = max($element); $matrixNum = $Result['matrix_num']; $max_matrixNum = max($matrixNum); What's wrong with my syntax and what's the best way to do this? Thanks a lot. T Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 14, 2007 Share Posted November 14, 2007 Is all you want the maximum values? Use the mysql function: $sql= "SELECT MAX(element_key) AS max_element_key, MAX(matrix_num) AS max_matrix_num FROM matrix"; Quote Link to comment Share on other sites More sharing options...
tmyonline Posted November 14, 2007 Author Share Posted November 14, 2007 Hi Ben, I understand your SQL. It is very efficient but then what ?! So I have: $sql = "SELECT MAX(element_key) AS max_element_key, MAX(matrix_num) AS max_matrix_num FROM matrix"; $result = mysql_db_query($db,$sql,$cid); I tried to do: $max_element = $result['max_element_key']; $max_matrixNum = $result['max_matrix_num']; echo $max_element . '<br />'; echo $max_matrixNum but I didn't see anything ! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 14, 2007 Share Posted November 14, 2007 Well, you're using a user defined function, mysql_db_query - i dont know what it returns - does it return an array?. If you're not sure either, then try: $sql = "SELECT MAX(element_key) AS max_element_key, MAX(matrix_num) AS max_matrix_num FROM matrix"; $result = mysql_db_query($db,$sql,$cid); var_dump($result); And see what you get Quote Link to comment Share on other sites More sharing options...
tmyonline Posted November 14, 2007 Author Share Posted November 14, 2007 Hi Ben, I did as you suggested. $sql = "SELECT MAX(element_key) AS max_element_key, MAX(matrix_num) AS max_matrix_num FROM matrix"; $matrixResult = mysql_db_query($db,$sql,$cid); $result = var_dump($matrixResult); echo $result; echo $result['max_element_key']; Here's the output: resource(5) of type (mysql result) which I have no idea what it is! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 14, 2007 Share Posted November 14, 2007 Ok, its returning the same thing that a normal mysql_query would do - a result resource. Try: $sql = "SELECT MAX(element_key) AS max_element_key, MAX(matrix_num) AS max_matrix_num FROM matrix"; $matrixResult = mysql_db_query($db,$sql,$cid); $result = mysql_fetch_assoc($maxtrixResult); echo $result['max_element_key']; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 14, 2007 Share Posted November 14, 2007 You need to fetch the results after doing the query: <?php $sql = "SELECT MAX(element_key) AS max_element_key, MAX(matrix_num) AS max_matrix_num FROM matrix"; $rs = mysql_query($sql); $rw = mysql_fetch_assoc($rs); echo $rw['max_element_key'] . ' ... ' . $rw['max_matrix_num'] . '<br>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
tmyonline Posted November 14, 2007 Author Share Posted November 14, 2007 Thanks Ben. That works! Thanks a lot. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 14, 2007 Share Posted November 14, 2007 No problem - glad to help. 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.