0

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.

scleveland04
  • 19
  • 1
  • 1
  • 2

2 Answers2

6

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){
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Thank you so much John! That worked. I had a feeling it had to do with the scope, but my still learning PHP didn't know where to look. – scleveland04 Sep 21 '13 at 14:55
2

$adConn is in the global scope and you cannot access it from the function, if you change $adConn to $GLOBALS['adConn'] it should work.

sangi93
  • 353
  • 1
  • 3
  • 7