将Mysql_Query转换为Mysqli

时间:2017-03-18 11:09:21

标签: php mysqli

我正在尝试使用Mysql_Query修改以下函数到Mysqli。

我似乎在某个地方出错了,但我没有得到任何错误,只是没有完成任何结果。

任何人都可以帮我解决我的错误吗?

原始代码

<?php
function get_product_name($pid){
$result=mysql_query("select Product_Name from Products where Product_ID=$pid")  or die("select Product_Name from Products where Product_ID=$pid"."<br/> <br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['Product_Name'];
}
function get_price($pid){
$result=mysql_query("select ProductPrice from Products where Product_ID=$pid") or die("select ProductPrice from Products where Product_ID=$pid"."<br/> <br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['ProductPrice'];
}
?>

更新代码

<?php
function get_product_name($pid){
$result=$mysqli->query("select Product_Name from Products where Product_ID=$pid") or die("select Product_Name from Products where Product_ID=$pid"."<br/><br/>".mysql_error());
$row=$result->fetch_array();
return $row['Product_Name'];
}
function get_price($pid){
$result=$mysqli->query("select ProductPrice from Products where Product_ID=$pid") or die("select ProductPrice from Products where Product_ID=$pid"."<br/><br/>".mysql_error());
$row=$result->fetch_array();
return $row['ProductPrice'];
}
?>

1 个答案:

答案 0 :(得分:0)

inside函数声明global $mysqli;以调用全局变量mysqli object

<?php
function get_product_name($pid){
global $mysqli;
$result=$mysqli->query("select Product_Name from Products where Product_ID=$pid") or die("select Product_Name from Products where Product_ID=$pid"."<br/><br/>".mysql_error());
$row=$result->fetch_array();
return $row['Product_Name'];
}
function get_price($pid){
global $mysqli;
$result=$mysqli->query("select ProductPrice from Products where Product_ID=$pid") or die("select ProductPrice from Products where Product_ID=$pid"."<br/><br/>".mysql_error());
$row=$result->fetch_array();
return $row['ProductPrice'];
}
?>
相关问题