2017-03-18 108 views
0

我正在嘗試使用Mysql_Query將下面的函數改爲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']; 
} 
?> 
+0

內部函數聲明'全球$ mysqli的;'調用全局變量mysqli對象 –

+1

真棒謝謝你。現在完美的作品:) – Trotterwatch

回答

0

內部函數聲明global $mysqli;調用全局變量的mysqli對象

<?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']; 
} 
?> 
+0

好,如果我的回答是正確的,請將其標記爲已接受...看看這裏如何https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –