在mysqli中循环遍历结果 [英] Looping through results in mysqli

查看:182
本文介绍了在mysqli中循环遍历结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mysqli的新手,在遍历mysqli的结果时遇到问题.不幸的是,我只得到一个结果.当我将查询放入phpMyAdmin时,它会给出三个结果.我相信相关的代码在这里,我只是在说错了:

$connection = new mysqli($host, $databaseUsername, $databasePassword, $database);

if ($connection->connect_errno > 0) {
    die ('Unable to connect to database [' . $connection->connect_error . ']');
}

$sql = "SELECT clientId, studentFirstName, studentLastName
        FROM clients
        WHERE (studentEmail = '$postEmail') OR (parentEmail = '$postEmail');";  

if (!$result = $connection->query($sql)) {
    die ('There was an error running query[' . $connection->error . ']');
}

echo '<select class = "toolbarDropdown" id = "toolbarDropdown-MultipleAccounts">';

    while ($row = $result->fetch_array()) {
        echo '<option value="'.$row["clientId"].'">'.$row["studentFirstName"].' '.$row["studentLastName"].'</option>';
    }

echo '</select>';

解决方案

您缺少HTML中option ="value<-处的结尾处

请注意

$row = $result->fetch_array()

可以替换为

$row = $result->fetch_assoc()

这样做,获取的每个记录的数组将占用一半的大小.

I am new to mysqli and having a problem looping through results with mysqli. Unfortunately, I am only getting a single result. When I put the query into phpMyAdmin, it comes up with three results. I believe the relevant code is here and that I am just calling it wrong:

$connection = new mysqli($host, $databaseUsername, $databasePassword, $database);

if ($connection->connect_errno > 0) {
    die ('Unable to connect to database [' . $connection->connect_error . ']');
}

$sql = "SELECT clientId, studentFirstName, studentLastName
        FROM clients
        WHERE (studentEmail = '$postEmail') OR (parentEmail = '$postEmail');";  

if (!$result = $connection->query($sql)) {
    die ('There was an error running query[' . $connection->error . ']');
}

echo '<select class = "toolbarDropdown" id = "toolbarDropdown-MultipleAccounts">';

    while ($row = $result->fetch_array()) {
        echo '<option value="'.$row["clientId"].'">'.$row["studentFirstName"].' '.$row["studentLastName"].'</option>';
    }

echo '</select>';

解决方案

You are missing the closing " at option="value <-- in your HTML

Note that

$row = $result->fetch_array()

can be replaced by

$row = $result->fetch_assoc()

Doing so, the array for each record you fetch would take half of the size.

这篇关于在mysqli中循环遍历结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆