将“ foreach”循环转换为:“ for,while和while”

时间:2019-11-27 17:05:08

标签: php mysql

我正在尝试将此foreach转换为所有循环,以便从屏幕上的msyql咨询打印阵列。

$con = mysqli_connect("localhost", "root" ,"" ,"base");
if($con->connect_error)
{
    die($con->mysqli_connect_error());
}

print_r ("conexion exitosa");

$sql = "SELECT code, name, grade FROM teachers WHERE status = 1 ";
$result = $con->query($sql); 
$row = mysqli_fetch_array($result); //array

**foreach ($result as $row) //array as value
    {
        print_r($row); //print value
    }**
?> ```

1 个答案:

答案 0 :(得分:0)

您还需要执行一个步骤才能将MySQL返回的行转换为数组。

while ($row = mysqli_fetch_assoc($result)) { var_dump($row); }

您还可以回显一行中的每个元素。例如:

echo $row['myfield'];在while循环内。