duancui19840401 2014-12-13 21:27
浏览 473

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

I am getting the PHP warning:

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

Printed to my error_log file however the query is running fine and the results are executing correctly. 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. It works fine and everything is printing correctly. Any idea why it would think $link is null.

to the comment from 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:

$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
}
  • 写回答

2条回答 默认 最新

  • duankuiyu4618 2015-03-18 19:35
    关注

    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.

    Second possibility is that $link is overriden in next lines with some simple mistake like:

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c