开发者

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

开发者 https://www.devze.com 2023-02-15 20:06 出处:网络
Question edited following the comments. It still doesn\'t work. Hi there, I\'m trying to learn how PDO works, but my script:

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

精彩评论

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