2

I am very new to PHP. I am just creating a simple page that fetch the data from database and generate the XML. There are two tables one having the competitor table and second has ranking for these competitors, So i am able to fetch the candidate data and when i am looping to this data and try to hit to DB again in the loop, getting following Error:

Undefined variable: mysqli in

Fatal error: Call to a member function query() on null in

I have tried few things but that didn't work. here is my code:

<?php
/** create XML file */ 
$mysqli = new mysqli("localhost", "root", "******", "****");
/* check connection */
if ($mysqli->connect_errno) {
   echo "Connect failed ".$mysqli->connect_error;
   exit();
}else{
}
$query = "select * from competitors where eventid=290 order by 1 desc LIMIT 0, 10;";
$booksArray = array();
if ($result = $mysqli->query($query)) {
/* fetch associative array */
   while ($row = $result->fetch_assoc()) {
   array_push($booksArray, $row);
  }
   if(count($booksArray)){
createXMLfile($booksArray);

 }
/* free result set */
   $result->free();
}
function createXMLfile($booksArray){
   $filePath = 'book.xml';
   $dom     = new DOMDocument('1.0', 'utf-8'); 
   $root      = $dom->createElement('books'); 
   for($i=0; $i<count($booksArray); $i++){
$eventid        =  $booksArray[$i]['eventid'];  
 $fee      =  $booksArray[$i]['fee']; 
$competitorid_system    =  $booksArray[$i]['competitorid'];
$datetimeentered     =  $booksArray[$i]['datemodified']; 
// $bookISBN      =  $booksArray[$i]['ISBN']; 
//  $bookCategory  =  $booksArray[$i]['category'];  
$book = $dom->createElement('book');
 $book->setAttribute('eventid', $eventid);
$name     = $dom->createElement('fee', $fee); 
$book->appendChild($name); 
$author   = $dom->createElement('competitorid_system', $competitorid_system); 

 $book->appendChild($author); 

 $price    = $dom->createElement('datetimeentered', $datetimeentered); 

$book->appendChild($price); 

// fetch other data
$query = "select * from ranking where eventid=290 and competitorid=".$competitorid_system;;

 echo "Query".$query;
   //exit();
$booksRankingArray = array();
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
array_push($booksRankingArray, $row);
}
if(count($booksRankingArray)){
createXMLfile($booksRankingArray);
 }
/* free result set */
$result->free();
}
$root->appendChild($book);
}
$dom->appendChild($root); 
$dom->save($filePath); 
} 
/* close connection */
$mysqli->close();
Ram Singh
  • 5,979
  • 29
  • 93
  • 156
  • 1
    Please indent your code properly. It's very hard to read and follow the code when the indentations are totally random like that. – Magnus Eriksson Nov 27 '18 at 06:31

1 Answers1

3

The $mysqli variable is out out of scope for createXMLfile().

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

Please refer to the PHP Manual to understand the variable scope.

Tol solve the issue, change the function signature to

function createXMLfile($mysqli, $booksArray)
{ 
    // … rest of code …
}

And then pass the variable from the outer scope to the function scope:

$mysqli = new mysqli("localhost", "root", "******", "****");
$booksArray = array();    
createXMLfile($mysqli, $booksArray);

You can also use a global variable to pull in variables from the global scope:

function createXMLfile($booksArray)
{ 
    global $mysqli;
    // … rest of code …    
}

But using global variables is generally discouraged because it leads to tightly coupled code and makes the code less easy to reason about.

Gordon
  • 296,205
  • 68
  • 508
  • 534