循环命中数据库 php
Hit to Database in loop php
我对 PHP 很陌生。我只是创建一个简单的页面,从数据库中获取数据并生成 XML。有两个 table,一个有竞争对手 table,第二个有这些竞争对手的排名,所以我能够获取候选数据,当我循环到这些数据并尝试再次访问数据库时在循环中,出现以下错误:
Undefined variable: mysqli in
Fatal error: Call to a member function query() on null in
我尝试了一些方法,但都没有用。这是我的代码:
<?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();
$mysqli
变量超出了 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.
为了解决问题,将函数签名更改为
function createXMLfile($mysqli, $booksArray)
{
// … rest of code …
}
然后将变量从外部范围传递到函数范围:
$mysqli = new mysqli("localhost", "root", "******", "****");
$booksArray = array();
createXMLfile($mysqli, $booksArray);
您还可以使用全局变量从全局范围中提取变量:
function createXMLfile($booksArray)
{
global $mysqli;
// … rest of code …
}
But using global variables is generally discouraged 因为它会导致紧密耦合的代码并使代码不那么容易推理。
我对 PHP 很陌生。我只是创建一个简单的页面,从数据库中获取数据并生成 XML。有两个 table,一个有竞争对手 table,第二个有这些竞争对手的排名,所以我能够获取候选数据,当我循环到这些数据并尝试再次访问数据库时在循环中,出现以下错误:
Undefined variable: mysqli in
Fatal error: Call to a member function query() on null in
我尝试了一些方法,但都没有用。这是我的代码:
<?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();
$mysqli
变量超出了 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.
为了解决问题,将函数签名更改为
function createXMLfile($mysqli, $booksArray)
{
// … rest of code …
}
然后将变量从外部范围传递到函数范围:
$mysqli = new mysqli("localhost", "root", "******", "****");
$booksArray = array();
createXMLfile($mysqli, $booksArray);
您还可以使用全局变量从全局范围中提取变量:
function createXMLfile($booksArray)
{
global $mysqli;
// … rest of code …
}
But using global variables is generally discouraged 因为它会导致紧密耦合的代码并使代码不那么容易推理。