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();
 
     
    