简体   繁体   中英

How to fetch and display data at the same time from MYSQL using PHP

I am unable to display data from a table from a MYSQL database using PHP OOP. However, I'm not sure if I'm unable to display the data because I'm not actually fetching the data in the first place.

I've tried fetching and displaying the data using only a PHP array and no HTML in my method and I figured this wouldn't be working because I wasn't using HTML list tags to format the data from the database. I've considered using a HTML table but I have seen displays from databases using lists work a few times before and I want to know why this doesn't work how it should.

I've tested for MYSQL connection and it does exist.

* M_PRODUCTS.PHP *

<?php  

class Products 
{
    private $Conn;
    private $db_table = "toys"; 

    function __construct() {
        // here we're making sure that the connection from $conn in "init.php" is transferred 
        // into our own private $conn property for usage in this object
        global $Conn; 
        $this->Conn = $Conn;
    }

        // fetches and displays all products from db
        public function fetch_all_products($id = NULL)
        { 
            if ($id == NULL)
            {
                $data = []; 
                if ($result = $this->Conn->query("SELECT * FROM " . $this->db_table . " ORDER BY name"))
                {
                    if ($result->num_rows > 0)
                    {
                        while ($row = $result->fetch_array())
                        {
                            $data = array(
                            "id" => $row["product_id"],
                            "name" => $row["name"],
                            "price" => $row["price"],
                            "image" => $row["image"]
                            ); 
                        } 
                        return $data; 
                    }
                else 
                {
                    return "<h1>Oops... Something went wrong!</h1>"; 
                }
            }
        }
    }
}

* INDEX.PHP *

<?php include("init.php"); 
      include("models/m_products.php"); 
?>

<body>
    <div id="whitespace">

        <?php

$products = fetch_all_products();

?>
      <?php foreach($products as $row) { ?>
      <tr>
        <td><?php echo $row->name; ?></td>
        <td><?php echo $row->price; ?></td>
        <td><img src="<?php echo $row->image_path(); ?>" alt="<?php echo $row->name; ?>" width="100" /></td>

      </tr>
      <?php } ?>

    </div>
</body>

I expect the images of my products to be displaying in my index.html file. However, nothing appears.

I also get this error message in the JavaScript console: Failed to load resource: the server responded with a status of 404 (Not Found). This would explain a lot but as I said I tested my database connection and it works. I'm not sure how or why this message is coming from the JavaScript console if I'm not using JavaScript. I thought it would be worth mentioning anyway.

根据你的代码,函数返回一些数据,但问题是你没有打印它,所以不是在函数中return你可以使用echo或只是把

echo $Products->fetch_all_products();

Strange code, I would do something like that. 1. First the function find all() 2. index.php echo - output

<?php

$products = find_all_products();

?>
      <?php foreach($products as $row) { ?>
      <tr>
        <td><?php echo $row->name; ?></td>
        <td><?php echo $row->price; ?></td>
        <td><img src="<?php echo $row->image_path(); ?>" width="200" /></td>

      </tr>
      <?php } ?>

Correct your comparison operator it should be. if ($id == NULL) instead of

if ($id = NULL)

in your function fetch_all_products() .

correct the code in index.php to print an array use print_r() or loop through the result array.

There are 2 major issues I can see in your code 1) you need to update your comparison operator from

if ($id = NULL)

to

if ($id == NULL)

2) you need to update your code in index.php from

<body>
    <div id="whitespace">
        <h1><?php echo shop_name ?></h1>

        <?php

        echo $Products->fetch_all_products();

        ?>
    </div>
</body>

to

<body>
    <div id="whitespace">

        <?php

$products = find_all_products();

?>
      <?php foreach($products as $row) { ?>
      <tr>
        <td><?php echo $row->name; ?></td>
        <td><?php echo $row->price; ?></td>
        <td><img src="<?php echo $row->image_path(); ?>" alt="<?php echo $row->name; ?>" width="100" /></td>

      </tr>
      <?php } ?>


    </div>
</body>

I hope it will sort out your issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM