简体   繁体   中英

Loop MySQLi Prepared Statement with PHP OOP

Hello I'm trying to loop through all the rows in my database table using prepared statement written in PHP every time i use while loop on the fetch() function it gets me in an infinite loop here is my code please help

public function loadIndex() {
    $conn = new Database();
    $db = $conn->Connect();
    $query = "
        SELECT Title,Description,uploadeddate
        FROM article
        ORDER BY article.uploadeddate DESC LIMIT 10";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($title, $desc, $date);
    $num_rows = $stmt->num_rows();
    $fetch = $stmt->fetch();
    $data = array(
    'title' => $title,
    'desc' => $desc,
    'date' => $date,
        'num_rows' => $num_rows,
    'fetch' => $fetch
    );
    return $data;
}

And Here is the Use

<?php
  art = new Articles();
  $index = $art->loadIndex();
  $num =  $index['num_rows'];
  if($num != 0) {
while() {$index['fetch']} {
    echo $index['title']."<br />";
    echo $index['desc']."<br />";
    echo $index['date']."<br />";
   }
   }?>

Thanks

A Complete mess!

Forgetting about Your Common Sense's very valid point for now.

Not tested but try this for a starter for 10.

The Article Class

public function loadIndex() {
    $conn = new Database();
    $db = $conn->Connect();

    $title = '';
    $desc = '';
    $date = '';
    $data = array();

    $query = "SELECT Title,Description,uploadeddate
              FROM article
              ORDER BY article.uploadeddate DESC LIMIT 10";

    $stmt = $db->prepare($query);
    $stmt->execute();

    $stmt->bind_result($title, $desc, $date);

    while ( $stmt->fetch() ) {

        $data[] = array(
                   'title'    => $title,
                   'desc'     => $desc,
                   'date'     => $date                      
                  );
    }

    return $data;
}

Main Code calling Article->loadIndex()

<?php
    art = new Articles();
    $index = $art->loadIndex();

    // you dont need to know how big the $index array is here
    // if its empty then the foreach will just not run.

    foreach ($index as $row) {
        echo $row['title'] . '<br />';
        echo $row['desc'] . '<br />';
        echo $row['date'] . '<br />';
   }
?>

In your index:

While($stmt->fetch())
{
$index =count($data);
$data['title'][$index] = $title;
...
}

First of all you should make a database connection once only and not in every function call. Once connected you can just execute the queries inside the function and hence no need to connect every time.

Secondly the infinite loop is caused because you have no condition inside while loop.

So change this:

while() {$index['fetch']}

to

while($index['fetch'])

Thanks Ahmar

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