mrherman Posted July 6, 2010 Share Posted July 6, 2010 Greetings, I have a simple MySQL query which give me only one result, an integer. Is there a simpler way to extract the value of "nMaxunit" rather than having to do a "while" loop and a "foreach" loop? A second question is that "{$c_keytable}" seems to work the same as $c_keytable (without the brackets). Which is preferable? Thanks! $sql = "SELECT MAX( riounit ) AS nMaxunit FROM {$c_keytable}" ; $result = mysql_query ( $sql ) or die ("no result") ; while ( $row = mysql_fetch_array ( $result ) ) { foreach ( $row as $key => $value ) { $max_riounit = $value ; } } Link to comment https://forums.phpfreaks.com/topic/206840-l/ Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2010 Share Posted July 6, 2010 You can simply not include the loop, or use mysql_result(). Link to comment https://forums.phpfreaks.com/topic/206840-l/#findComment-1081721 Share on other sites More sharing options...
kenrbnsn Posted July 6, 2010 Share Posted July 6, 2010 Just get the value: <?php $sql = "SELECT MAX( riounit ) AS nMaxunit FROM {$c_keytable}" ; $result = mysql_query ( $sql ) or die ("no result") ; $rw = mysql_fetch_assoc($result); $max_riounit = $rw['riounit']; ?> Ken Link to comment https://forums.phpfreaks.com/topic/206840-l/#findComment-1081722 Share on other sites More sharing options...
mrherman Posted July 6, 2010 Author Share Posted July 6, 2010 Wow, that was fast. Thanks for the replies. I looked at mysql_result, but couldn't hang with it mentally. What would the solution look like if I used "mysql_result()"? Also, what about the brackets in the variable table name? Link to comment https://forums.phpfreaks.com/topic/206840-l/#findComment-1081725 Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2010 Share Posted July 6, 2010 It's been a long time since I've used mysql_result() myself; I use mysql_fetch_assoc() for nearly everything. I didn't test this, but I think it's a correct example $query = "SELECT MAX(`id`) FROM table"; $result = mysql_query($query); $value = mysql_result($result); Link to comment https://forums.phpfreaks.com/topic/206840-l/#findComment-1081729 Share on other sites More sharing options...
mrherman Posted July 6, 2010 Author Share Posted July 6, 2010 Thanks! That helps -- I'll experiment with it. Link to comment https://forums.phpfreaks.com/topic/206840-l/#findComment-1081919 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.