gpt4 book ai didi

php - mysqli_query() 参数 1 错误但查询正确执行

转载 作者:行者123 更新时间:2023-11-30 22:53:10 29 4
gpt4 key购买 nike

我收到 PHP 警告:

mysqli_query() expects parameter 1 to be mysqli, null given

打印到我的 error_log 文件,但是查询运行正常并且结果执行正确。我真的很想了解为什么我会收到警告。下面的代码

<?
//initialize file sets $link variable with mysqli_connect() and contains the $item variable
require "php/initialize.php";

$tradeAmount = mysqli_query($link, "SELECT sum(amt) AS total FROM actfcast WHERE item=$item");
$tradeAmount_array = mysqli_fetch_array($tradeAmount);
?>

然后我遍历 $tradeAmount_array 并回显它。它工作正常,一切都在正确打印。知道为什么它会认为 $linknull

来自 Rizier123 的评论:

object(mysqli)#1 (19) { ["affected_rows"]=> int(1) ["client_info"]=> string(11) "5.5.38-35.2" ["client_version"]=> int(50538) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(1) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(11) "5.5.40-36.1" ["server_version"]=> int(50540) ["stat"]=> string(152) "Uptime: 962856 Threads: 5 Questions: 252975480 Slow queries: 513 Opens: 319250 Flush tables: 1 Open tables: 20000 Queries per second avg: 262.734" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(4660775) ["warning_count"]=> int(0) }

来自 initialize.php 文件:

$host="localhost";
$current_db="xxxxxx";
$current_dir="exp";
$dbuser="xxxxxxx";
$dbpw="xxxxxxx";

if (!(isset($_SESSION['user']) && $_SESSION['user'] != '')){
$_SESSION['redirect'] = $_SERVER['REQUEST_URI'];
header ("Location: http://www.xxxxx.com/".$current_dir."/signin.php");
}
else{
$link = mysqli_connect($host, $dbuser, $dbpw, $current_db);
//more stuff
}

最佳答案

我的第一个盲目猜测是 $link 变量是在函数或类方法中创建的,因此它在 mysqli_query 行中不可见。

第二种可能性是 $link 在下一行被一些简单的错误覆盖,例如:

if ($link = null)
{
echo "Connection not established";
}

关于php - mysqli_query() 参数 1 错误但查询正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27463796/

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