codeguy / php-the-right-way

An easy-to-read, quick reference for PHP best practices, accepted coding standards, and links to authoritative tutorials around the Web

Home Page:https://www.phptherightway.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interacting with Databases code example

s3b4stian opened this issue · comments

Hello!

This code example in http://www.phptherightway.com/#databases_interacting could be changed, for performance reasons, from this:

function getAllFoos($db) {
    return $db->query('SELECT * FROM table');
}

foreach (getAllFoos($db) as $row) {
    echo "<li>".$row['field1']." - ".$row['field1']."</li>"; // BAD!!
}

to this:

function getAllFoos($db) {
    return $db->query('SELECT * FROM table');
}

$result = getAllFoos($db);

foreach ($result as $row) {
    echo "<li>".$row['field1']." - ".$row['field1']."</li>"; // BAD!!
}

Done. Thanks.

Hi check the snippet on the web page, there is an extra s

$results = getAllFoos($db);
foreach ($resultss as $row) {
    echo "<li>".$row['field1']." - ".$row['field1']."</li>"; // BAD!!
}

ha I must have fat-fingered that one. Just pushed a fix.