简体   繁体   中英

Find the highest number in a column

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`

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM