I have two files:
cart_function.php
function get_product_name($pid){
$result = mysql_query("SELECT product_name FROM product_table WHERE product_id='".$pid."'", $link);
$row = mysql_fetch_array($result);
return $row['product_name'];
}
cart.php
<?php
include('cart_function.php');
$pid = $_GET['product_id'];
$pname = get_product_name($pid);
echo $pname;
?>
After I execute the cart.php, it shows an error
supplied argument is not a valid MySQL-Link resource'
If I run the query directly in MySQL database it works perfectly. If I used the get_product_name() function directly in cart.php, again it's not working. But if I remove the function and use the code below, it works:
<?php
$pid = $_GET['product_id'];
$result = mysql_query("SELECT product_name FROM product_table WHERE product_id='".$pid."'",$link);
$row = mysql_fetch_array($result);
$pname = $row['product_name'];
echo $pname;
?>
Why?
$link, you have to define it as global inget_product_name:global $link;get_product_name()has access to$link?