Why does this query only show one result?

后端 未结 3 1412
闹比i
闹比i 2021-01-24 08:19

The query below will be used a search script. For some reason it won\'t return all results where either condition is true. What am i doing wrong?

$sql = \"SELE         


        
3条回答
  •  情话喂你
    2021-01-24 08:31

    You are printing outside of while. Which means, no matter how many results you have, only the one will be printed.

    Either print inside the loop

    while($row = mysql_fetch_array($query))
    {
        $name = htmlspecialchars($row['name']);
        $code = htmlspecialchars($row['id_code']);
        print("$code: $name

    "); }

    or collect the variables in an array while looping and use them after the loop as you like

    $result_array = array();
    while($row = mysql_fetch_array($query))
    {
        $name = htmlspecialchars($row['name']);
        $code = htmlspecialchars($row['id_code']);
    
        $result_array[] = array(
            'name' => $name,
            'code' => $code
        );
    }
    print_r($result_array);
    

提交回复
热议问题