我正在尝试将以下脚本从mysql转换为mysqli,但遇到了问题。
这是使用mysql的原始脚本:
//database connection
$dbname = 'nursery';
$link = mysql_connect("localhost","root","") or die("Couldn't make connection.");
$db = mysql_select_db($dbname, $link) or die("Couldn't select database");
?>
<div style="width:728px;margin:auto;">
<div id='cssmenu'>
<ul>
<?php
function query($parentid) { //function to run a query
$query = mysql_query ( "SELECT * FROM menu WHERE parentid=$parentid" );
return $query;
}
function has_child($query) { //This function checks if the menus has childs or not
$rows = mysql_num_rows ( $query );
if ($rows > 0) {
return true;
} else {
return false;
}
}
function fetch_menu($query) {
while ( $result = mysql_fetch_array ( $query ) ) {
$menu_id = $result ['id'];
$title = $result ['title'];
$url = $result ['url'];
echo "<li class='has-sub '><a href='{$url}'><span>{$title}</span></a>";
if (has_child ( query ( $menu_id ) )) {
echo "<ul>";
fetch_menu ( query ( $menu_id ) );
echo "</ul>";
}
echo "</li>";
}
}
fetch_menu (query(0)); //call this function with 0 parent id
?>
</ul>
</div>
</div>
所以我试图把它转换成我的网站其余部分使用的mysqli并且已经走到了这一步:
<?php
require("../login/common.php");
//database connection;
include '../connect.php';
?>
<div style="width:728px;margin:auto;">
<div id='cssmenu'>
<ul>
<?php
function query($parentid) { //function to run a query
$query = mysqli_query ($db, "SELECT * FROM menu WHERE parentid=$parentid" );
return $query;
}
function has_child($query) { //This function checks if the menus has childs or not
$rows = mysqli_num_rows ( $query );
if ($rows > 0) {
return true;
} else {
return false;
}
}
function fetch_menu($query) {
while ( $result = mysqli_fetch_array ( $query ) ) {
$menu_id = $result ['id'];
$title = $result ['title'];
$url = $result ['url'];
echo "<li class='has-sub '><a href='{$url}'><span>{$title}</span></a>";
if (has_child ( query ( $menu_id ) )) {
echo "<ul>";
fetch_menu ( query ( $menu_id ) );
echo "</ul>";
}
echo "</li>";
}
}
fetch_menu (query(0)); //call this function with 0 parent id
?>
</ul>
</div>
</div>
问题是我遇到了各种错误并且我不完全确定原因,我知道db错误可能是$ db没有被传递到函数中,但即使我写函数fetch_menu($ query, $ db){我仍然得到同样的错误?。
Notice: Undefined variable: db in C:\easyphpserver\data\localweb\projects\nursery\menu\menu3.php on line 147
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\easyphpserver\data\localweb\projects\nursery\menu\menu3.php on line 147
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\easyphpserver\data\localweb\projects\nursery\menu\menu3.php on line 159
connect.php的内容是:
<?php
$db = new mysqli('localhost', 'root', '', 'nursery');
$db->set_charset('utf8mb4');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
?>
答案 0 :(得分:0)
将$db
传递给query()
function query($parentid, $db) {
$query = mysqli_query ($db, "SELECT * FROM menu WHERE parentid=$parentid" );
return $query;
}
答案 1 :(得分:0)
好的,所以这个解决方案似乎有效,通过将db连接放在函数内部,如果我通过键入function query($ parentid,$ db){在函数中包含$ db}就可以了。 这不起作用并导致错误,但如果我把数据库连接放在函数内部就可以了.....非常奇怪。
<?php
//error_reporting(0);
require("../login/common.php");
//database connection;
include '../connect.php';
?>
<div style="width:728px;margin:auto;">
<div id='cssmenu'>
<ul>
<?php
function query($parentid) {
$db = new mysqli('localhost', 'root', '', 'nursery');
$db->set_charset('utf8mb4');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
//function to run a query
$query = mysqli_query ($db, "SELECT * FROM menu WHERE parentid=$parentid" );
return $query;
}
function has_child($query) { //This function checks if the menus has childs or not
$rows = mysqli_num_rows ( $query );
if ($rows > 0) {
return true;
} else {
return false;
}
}
function fetch_menu($query) {
while ( $result = mysqli_fetch_array ( $query ) ) {
$menu_id = $result ['id'];
$title = $result ['title'];
$url = $result ['url'];
echo "<li class='has-sub '><a href='{$url}'><span>{$title}</span></a>";
if (has_child ( query ( $menu_id ) )) {
echo "<ul>";
fetch_menu ( query ( $menu_id ) );
echo "</ul>";
}
echo "</li>";
}
}
fetch_menu (query(0)); //call this function with 0 parent id
?>
</ul>
</div>
</div>