开发者

My $query->fetch() doesn't work

Question edited following the comments. It still doesn't work.

Hi there,

I'm trying to learn how PDO works, but my script:

$database = new PDO('mysql:host=localhost;dbname=***', '***', '***');
$query = $database->prepare("SELECT nombre, 
                                    tecnica,
                                    tamanno,
                                    estado FROM obra WHERE anno = ?");
$query->execute(array('2009'));
while ($item = $query->fetch(PDO::FETCH_ASSOC)) {
    $item['nombre'];
}

Prints nothing.开发者_Go百科 If I do:

var_dump($query->fetch())

I get bool(false). After reading lots of examples I can't figure out what I'm doing wrong.

Thanks in advance.


while ($item = $query->fetch(PDO::FETCH_ASSOC)) {
    $item['nombre'];
}

Prints nothing.

That's because you're not actually doing anything with $item['nombre']. Try:

while ($item = $query->fetch(PDO::FETCH_ASSOC)) {
    print_r($item);
}

You should get your expected output.

If not, try adding this before your connection is opened:

PDO::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

and make sure that you have display_errors set to true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜