简体   繁体   English

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

[英]mysqli_query() parameter 1 error but query executes correctly

I am getting the PHP warning: 我收到PHP警告:

mysqli_query() expects parameter 1 to be mysqli, null given mysqli_query()期望参数1为mysqli,给定null

Printed to my error_log file however the query is running fine and the results are executing correctly. 打印到我的error_log文件中,但是查询运行正常并且结果正确执行。 I would just really like to understand why I'm getting the warning. 我真的很想了解为什么我收到警告。 code below 下面的代码

<?
//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);
?>

I then loop through the $tradeAmount_array and echo it. 然后,我遍历$tradeAmount_array并回显它。 It works fine and everything is printing correctly. 它工作正常,一切都可以正确打印。 Any idea why it would think $link is null . 知道为什么它会认为$linknull

to the comment from Rizier123: 来自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) }

From the initialize.php file: 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
}

My first blind guess would be that $link variable is created within a function or class method so it would not be visible in mysqli_query line. 我的第一个盲目猜测是$link变量是在函数或类方法中创建的,因此它在mysqli_query行中不可见。

Second possibility is that $link is overriden in next lines with some simple mistake like: 第二种可能性是$link在下一行中被覆盖,出现了一些简单的错误,例如:

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

You have a syntax error in your query: 您的查询中存在语法错误:

mysqli_query($link, "SELECT sum(amt) AS total FROM actfcast WHERE item='$item'");

$item must be in single qoutes. $item必须在单个qoutes中。

It is better you use prepared statements. 最好使用准备好的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM