Edward68 Edward68 - 1 year ago 64
PHP Question

Display heading from database using php

I am setting up a shopping website. I have a product display page that has a

heading for each product. I would like to pull the headings from my database but for some reason it is not working. I have a script called connect.php for the connection which is here

$servername = "localhost:3306";
$username = "theusername";
$password = "thepassword";
$db = "thedatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();

The database is connected. I would really appreciate it if someone could please tell me what I am doing wrong or how I should be going about this. Thanks.
and then I have the following script at the top of the product display page:

$result= mysqli_query($conn, "SELECT * FROM database.products");
$product_name = $result['prod_name'];

I would like to display the product name as the heading and have been using the following script to do so but it does not work:

echo "<h4 style='margin-left:10px;'>"."Product :" .$product_name .'</h4>';

Answer Source

Fetch the products first, put the headings in an array

$products = [];
while($row = mysqli_fetch_assoc ( $result )){
    $products[] = [
        'heading' => $row['prod_name'],

and see what you get



$result= mysqli_query($conn, "SELECT * FROM database.products");
while($row = mysqli_fetch_assoc ( $result )){
    echo "<h4 style='margin-left:10px;'>"."Product :" .$row['prod_name'] .'</h4>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download