gpt4 book ai didi

PHP MySQLi 在不执行 while 循环的情况下回显数组中的数据

转载 作者:可可西里 更新时间:2023-11-01 00:14:26 25 4
gpt4 key购买 nike

使用 MySQLi 时,我是否必须执行一种 while 循环,将查询中的实际数据放入变量数组?

$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

// Check if able to connect to database
if ($conn->connect_error) {
trigger_error("Database connection failed: " . $conn->connect_error, E_USER_ERROR);
}

$sql = "SELECT name FROM users WHERE email = '$email'";

$rs = $conn->query($sql);
$numRows = $rs->num_rows();

我总是这样做:

$rs->data_seek(0);
while($row = $rs->fetch_assoc()) {
$name = $row['name'];
}

echo $name;

当只有一行时,是否有更方便的方法来回显查询中的数据?

最佳答案

如果只有一行,则不需要循环。只是做:

$row = $rs->fetch_assoc();
$name = $row['name'];

关于PHP MySQLi 在不执行 while 循环的情况下回显数组中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20176563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com