gpt4 book ai didi

php - 在循环 php 中命中数据库

转载 作者:行者123 更新时间:2023-11-29 01:49:07 26 4
gpt4 key购买 nike

我是 PHP 的新手。我只是创建一个简单的页面,从数据库中获取数据并生成 XML。有两个表,一个有竞争对 watch ,第二个有这些竞争对手的排名,所以我能够获取候选数据,当我循环到该数据并尝试在循环中再次访问数据库时,出现以下错误:

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 - 在循环 php 中命中数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com