I am trying to find the highest number in a column out of several results, I am trying to use the following code but it isn't working as expected. If I have records numbered 1 to 7 I want to pick 7 as it has the highest number.
if (isset($_GET['questionnaireID'])) {$questionnaireID = $_GET['questionnaireID'];}else {$questionnaireID = '';}
include("connectmysqli.php");
$sql = <<<SQL
SELECT *
FROM `QuestionnaireQuestions`
WHERE questionnaireID = '$questionnaireID'
SQL;
if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']');}
while($row = $result->fetch_assoc()){
$questionnumber = array($row['questionnumber']);
echo max($questionnumber);
}
what it returns at the moment is 1234567
怎么样
SELECT MAX(questionnaireID) AS maxId FROM `QuestionnaireQuestions`
Alter you SQL query and use MAX() function. http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_max
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.