EmperorJazzy Posted April 22, 2011 Share Posted April 22, 2011 At present, all my queries return multiple records. Now I want to do a record count, and only return the one record. Current Code $query = "SELECT * FROM tblListField WHERE ListID = '" . $_SESSION['listid'] . "'"; $result = mysqli_query($dbc, $query) or die('Error executing SELECT * statement for tblListField - ' . $query); if(mysqli_num_rows($result)) { // at least one row, process the data from the query while ($row = mysqli_fetch_array($result)) { So the above runs through each record; what do I use to get the one record from the following SQL? (Note; I'm using mysqli_) $query = "SELECT COUNT(ItemField_1) WHERE ListID='" . $listid . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/234449-single-result-sql-code/ Share on other sites More sharing options...
Maq Posted April 22, 2011 Share Posted April 22, 2011 Do this calculation in MySQL using COUNT: http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html Quote Link to comment https://forums.phpfreaks.com/topic/234449-single-result-sql-code/#findComment-1204893 Share on other sites More sharing options...
EmperorJazzy Posted April 22, 2011 Author Share Posted April 22, 2011 Maq - Appreciate it; apologies, I omitted the SQL with the COUNT function (now included). The question is orientated around the PHP syntax. Quote Link to comment https://forums.phpfreaks.com/topic/234449-single-result-sql-code/#findComment-1204897 Share on other sites More sharing options...
Maq Posted April 22, 2011 Share Posted April 22, 2011 Maq - Appreciate it; apologies, I omitted the SQL with the COUNT function (now included). The question is orientated around the PHP syntax. Are you asking how to display the count result? If not, could you elaborate? I would change the query to alias the count (easier to reference & read): $query = "SELECT COUNT(ItemField_1) AS cnt WHERE ListID='" . $listid . "'"; In your PHP you would then reference 'cnt' from the $row array: while ($row = mysqli_fetch_array($result)) { echo $row['cnt']; } Quote Link to comment https://forums.phpfreaks.com/topic/234449-single-result-sql-code/#findComment-1204900 Share on other sites More sharing options...
EmperorJazzy Posted April 22, 2011 Author Share Posted April 22, 2011 So essentially there is no syntax similar to the following without having to use the WHILE loop each time; $query = ..... $result = ...... echo "Number of records = " . $result['cnt']; Or could I simply use the mysqli_num_rows()? Quote Link to comment https://forums.phpfreaks.com/topic/234449-single-result-sql-code/#findComment-1204917 Share on other sites More sharing options...
Maq Posted April 22, 2011 Share Posted April 22, 2011 So essentially there is no syntax similar to the following without having to use the WHILE loop each time; Then don't use a while loop. $row = mysqli_fetch_array($result); echo $row['cnt']; Or could I simply use the mysqli_num_rows()? COUNT returns a single row/value, so mysqli_num_rows() would always return 1. If you don't use COUNT, then using mysqli_num_rows() would be inefficient. It's better to use SQL, that's what it was designed for. Quote Link to comment https://forums.phpfreaks.com/topic/234449-single-result-sql-code/#findComment-1204921 Share on other sites More sharing options...
EmperorJazzy Posted April 22, 2011 Author Share Posted April 22, 2011 $row = mysqli_fetch_array($result); echo $row['cnt']; Exactly what I was looking for. Thank you.[/code] Quote Link to comment https://forums.phpfreaks.com/topic/234449-single-result-sql-code/#findComment-1204922 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.