0

I am trying to adapt the below function from using Mysql_Query to Mysqli.

I seem to be going wrong somewhere, but I don't get any errors it just doesn't pull through any results.

Can anyone help with what I am doing wrong please?

Original Code

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

Updated Code

<?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 Answers1

0

inside function declare global $mysqli; to call the global variable 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'];
}
?>
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70