Hi, following a suggestion to replace mysql api with PDO I got this bit of php to work
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'store';
$conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
$sql = " SELECT products_quantity FROM products WHERE products_model = 'JLP.1HP' ";
$result = $conn->query($sql);
$row = $result->fetch(PDO::FETCH_OBJ) ;
if ( $row->products_quantity == 0)
{
echo '<img src="images/SoldOut.jpg" width="32" height="32" alt="" >' ;
}
Then tried to put it in a function
function checkStockLevel($stockModel){
$sql = " SELECT products_quantity FROM products WHERE products_model = '$stockModel' ";
$result = $conn->query($sql);
$row = $result->fetch(PDO::FETCH_OBJ) ;
if ( $row->products_quantity == 0 )
{
echo '<img src="images/SoldOut.jpg" width="32" height="32" alt="" >' ;
}
}
echo checkStockLevel('$stockModel');
?>
This doesn't work because the variable $conn is not in the scope of the variable. So put that in there and all the variables that it requires.
function checkStockLevel($stockModel){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'store';
$conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
$sql = " SELECT products_quantity FROM products WHERE products_model = '$stockModel' ";
$result = $conn->query($sql);
$row = $result->fetch(PDO::FETCH_OBJ) ;
if ( $row->products_quantity == 0 )
{
echo '<img src="images/SoldOut.jpg" width="32" height="32" alt="" >' ;
}
$conn->close();
}
echo checkStockLevel('JLP.1HP');
The question I have is now I am opening and closing the database every time I call the function. Is that okay / desirable, or is there another approach that would be better?
Thanks,
jim