A newbie question. There must be an easy answer to this but I have spent hours searching, including here in StackOverflow (also in Duplicate Questions), to no avail. Please help.
I have a Table inside a Database which I am trying to iterate.
Simplified Table is as follows:
id(int,auto-increment), product_name(varchar), price1(int)
1, widget1, 1000
2, widget2, 900
etc.
when I use this code, it works
<?php
$connection = mysqli_connect("localhost", "root", "", "test");
$item = 'widget1';
$sql = "SELECT * FROM gym_eqpt WHERE product_name = '$item' ";
$results = mysqli_query($connection, $sql);
while($result = mysqli_fetch_array($results)) {
echo "<p>item name: " . ($result['product_name']) . "<br />" . "<p>item price: " . number_format($result['price1']) . " </p>";
}
?>
However, when I try to use the same code inside a function, there is an "Undefined variable" error.
<?php
function iteratePrice($item) {
$sql = "SELECT * FROM gym_eqpt WHERE product_name = '$item' ";
$results = mysqli_query($connection, $sql);
while($result = mysqli_fetch_array($results)) {
echo "<p>item name: " . ($result['product_name']) ."<p>item price: " . number_format($result['price1']) . " </p>";
}
}
iteratePrice('widget1');
?>
Eventually, I would like to use this function inside different html div's on the same page, calling each product name and price in each div.