警告:mysqli_query()期望参数1为mysqli,给出null [英] Warning: mysqli_query() expects parameter 1 to be mysqli, null given

查看:4117
本文介绍了警告:mysqli_query()期望参数1为mysqli,给出null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这是重复的,但我找不到答案。无论如何,我无法查询我的数据库。



只是我试图调用一个函数,但出现错误...

 警告:mysqli_query()期望参数1为mysqli,null在第12行中给出

我的dbConnect.php文件包含

  $ adConn = mysqli_connect ( MYHOST, MYUSER, myPword, MYDB); 

我的functions.php文件包含

 需要dbConnect.php; 

//检查连接
if(mysqli_connect_errno($ adConn))
{
echo无法连接到MySQL:。 mysqli_connect_error();
}

// ----------------------------------- -------------- //
//获取特定周的游戏总数//
// ----------- -------------------------------------- //
函数GetGamesCount($ wID) {
$ sql =SELECT * FROM schedule WHERE weekID = $ wID;

$ gamesRS = mysqli_query($ adConn,$ sql);
$ games = mysqli_fetch_array($ gamesRS);


}

我从连线检查中得不到任何错误,我也尝试将它放在功能范围内。我知道我可以使用mysqli_num_rows函数,我会。我只需要弄清楚为什么它不会查询数据库。我试着用同样的问题与其他功能进行查询。但是,如果我在页面中查询或从单独的文件使用ajax,我没有任何问题。



我只是没有看到我错过了什么。因为 $ adConn 被宣布为外部的,所以我们非常感谢您的帮助。 的功能不在范围内。这意味着你不能在函数内部访问它。要访问它,您需要将它作为参数传递给函数。

 函数GetGamesCount($ wID,$ adConn){ 


I'm sure this is a duplicate, but I can not find an answer to this. For whatever reason, I can not query my database.

Simply I am trying to call a function but I get the error...

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in ... on line 12

My dbConnect.php file has

$adConn = mysqli_connect("myHost","myUser","myPword","myDB");

My functions.php files has

require "dbConnect.php";

// Check connection
if (mysqli_connect_errno($adConn))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//-------------------------------------------------//
//   Gets total number of games for a given week   //
//-------------------------------------------------//
function GetGamesCount($wID){
    $sql = "SELECT * FROM schedule WHERE weekID=$wID";

    $gamesRS = mysqli_query($adConn, $sql);
    $games = mysqli_fetch_array($gamesRS);


}

I get no error from the connection check, I have tried putting it within the funcation as well. I know I can use the mysqli_num_rows function, which I will. I just need to figure out why it won't query the database. I have tried querying with other functions with the same problem. However, if I query within the page or using ajax from a separate file, I have no problems.

I just don't see what I'm missing. Any help is greatly appreciated.

解决方案

Because $adConn is declared outside of the function is not in scope. That means you do not have access to it inside the function. To have access to it you need to pass it as aparameter of the function.

 function GetGamesCount($wID, $adConn){

这篇关于警告:mysqli_query()期望参数1为mysqli,给出null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆