简体   繁体   中英

PHP PDO, SQLITE and HTML

Hi I am trying to echo out rows from a table in my database by using a foreach loop

this is how i do it

//connecting to the database//

$stmt_seven = $db->prepare('SELECT img, description FROM Videos ORDER BY date LIMIT 10;');
$stmt_seven->execute();
$res_seven = $stmt_seven->fetchAll(PDO::FETCH_NUM);
<?php
    if(!$res_seven)
    {
        echo "<br />";
        echo"No Content Available Yet..";
    }
    else
        foreach($res AS $val)
        {
            foreach($val AS $val1)
            {
                **echo "$val1['img'];
                echo "$val1['description'];**    
            }
        }
    $db = null;
?>

the highlighted code does not work. And I do not know what i am doing wrong!

your $res is empty as per code. I guess it should be $res_seven

Remove the double quotes in echo "$val1['img']; echo "$val1['description'];

$stmt_seven = $db->prepare('SELECT img, description FROM Videos ORDER BY date LIMIT       
10;');
$stmt_seven->execute();
$res_seven = $stmt_seven->fetchAll(PDO::FETCH_NUM);
if(!$res_seven){
echo "<br />";
echo"No Content Available Yet..";
}
else
foreach($res AS $val) {
foreach($val AS $val1) {
**echo "$val1['img'];
echo "$val1['description'];**    
}
}
$db = null;
?>

Except for the typo's that others already pointed out, you are using PDO::FETCH_NUM which "returns an array indexed by column number as returned in your result set, starting at column 0".

Based on your code you want PDO::FETCH_ASSOC which "returns an array indexed by column name as returned in your result set"

Or, you could leave it blank and use the default PDO::FETCH_BOTH which "returns an array indexed by both column name and 0-indexed column number as returned in your result set"

Source: http://php.net/manual/en/pdostatement.fetch.php

$res_seven = $stmt_seven->fetchAll [...]
foreach($res as ...

what is $res?

foreach( $resultOfFetchAll as $val) {
  foreach($val as $val1) {
    // now $val1 is the value of a single column, i.e. a scalar or a string
    // but you're trying to access as if it was a row / array
  }
}

Instead of using fetchAll() and then iterating over that array you can simply pass the PDOStatement object itself to foreach since it implements the traversable interface. Eg

<?php
function foo($db) {
    $stmt = $db->query('SELECT img, description FROM Videos ORDER BY date LIMIT 10', PDO::FETCH_ASSOC);
    foreach( $stmt as $row ) {
        echo $row['img'], ' - ', $row['description'], "<br />\n";
    }
    if ( 0===$stmt->rowCount() ) {
        echo "no data yet<br />\n";
    }
    echo "---<br />\n";
}

$db = new PDO('sqlite::memory:', null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($db, false);
foo($db);
setup($db, true);
foo($db);

// boilerplate/data-source for self-contained example
function setup($pdo, $populate) {
    if ( !$populate ) {
        $pdo->exec('
            CREATE TABLE Videos (
                img,
                description,
                date
            )
        ');
    }
    else {
        $stmt = $pdo->prepare('INSERT INTO Videos (img,description,date) VALUES (?,?,?)');
        for($i=1; $i<21; $i++) {
            $stmt->execute(array(
                sprintf('img%02d', $i),
                sprintf('desc%02d', $i),
                sprintf('2012-01-%02d 12:00:00', $i)
            ));
        }
    }
}

prints

no data yet<br />
---<br />
img01 - desc01<br />
img02 - desc02<br />
img03 - desc03<br />
img04 - desc04<br />
img05 - desc05<br />
img06 - desc06<br />
img07 - desc07<br />
img08 - desc08<br />
img09 - desc09<br />
img10 - desc10<br />
no data yet<br />
---<br />

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