gpt4 book ai didi

php - MySqLi 由于某种原因无法工作

转载 作者:行者123 更新时间:2023-11-29 18:21:16 25 4
gpt4 key购买 nike

实际上,我正在尝试使用 MySqli,但它对我不起作用,即使它显示数据库已连接,但查询也不起作用,即使没有给出任何错误。我尝试使用下面的代码:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


$connect = mysqli_connect("localhost",'dbu', 'password');
if($connect){
echo 'connected';
}
else {
echo 'Failed:'.mysqli_error();
}
mysqli_select_db('dbname',$connect);

if($query = mysqli_query("SELECT * FROM `users`")){
$num = mysqli_num_rows($query);
while($fetch = mysqli_fetch_array($query)){
echo $id = $fetch['id'];
}
}
else {
echo 'error:'.mysqli_error();
}

最佳答案

打开错误,mysqli_* 函数中有错误。将您的代码更改为

$connect = mysqli_connect("localhost", 'dbu', 'password');
if ($connect) {
echo 'connected';
} else {
echo 'Failed:'.mysqli_connect_error();
}
mysqli_select_db($connect, 'dbname'); // <-------change this

if ($query = mysqli_query($connect, "SELECT * FROM `users`")) { //<---------------change this
$num = mysqli_num_rows($query);
while ($fetch = mysqli_fetch_array($query)) {
echo $id = $fetch['id'];
}
} else {
echo 'error:'.mysqli_error($connect);
}

关于php - MySqLi 由于某种原因无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46561713/

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