简体   繁体   English

PHP PDO,SQLITE和HTML

[英]PHP PDO, SQLITE and HTML

Hi I am trying to echo out rows from a table in my database by using a foreach loop 嗨,我正在尝试通过使用foreach循环从数据库中的表中回显行

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. 根据代码,您的$ res为空。 I guess it should be $res_seven 我想应该是$ res_seven

Remove the double quotes in echo "$val1['img']; echo "$val1['description']; 删除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". 除了其他人已经指出的错别字,您使用的是PDO::FETCH_NUM ,它“返回结果集中返回的以列号索引的数组,从列0开始”。

Based on your code you want PDO::FETCH_ASSOC which "returns an array indexed by column name as returned in your result set" 根据您的代码,您需要PDO::FETCH_ASSOC ,它“返回结果集中返回的按列名索引的数组”

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" 或者,您可以将其保留为空白,并使用默认的PDO::FETCH_BOTH ,它“返回结果集中返回的同时由列名和0索引列号索引的数组”

Source: http://php.net/manual/en/pdostatement.fetch.php 资料来源: http : //php.net/manual/en/pdostatement.fetch.php

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

what is $res? 什么是$ 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. 不必使用fetchAll()然后在该数组上进行迭代,只需将PDOStatement对象本身传递给foreach,因为它实现了可遍历的接口。 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 />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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