0

我试图检查我的代码不显示数据库中的数据的原因可能是什么,但似乎我不能。数据库中有数据但没有显示

<?php
    try {
    $con = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf8','root','');
    echo "we connected" ;
    } catch(PDOException $e){
    echo 'Connection failed'.$e->getMessage();
    }

    ?>
    <?Php

    $query ="select date_created from tish_user";
        $result= $con->prepare($query);
        $result->execute();
        while($row = $result->fetch(PDO::FETCH_ASSOC)){
    echo $row['date_created'];

    }
    ?>
4

1 回答 1

2
$con = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf8','root','');
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

查看错误。

您的查询有错误,前面没有空格from

(我还更改了 DSN 中的字符集以更正 mysql 拼写)

更一致的代码版本:

$dsn = 'mysql:host=localhost;dbname=tish_database;charset=utf8';
$opt = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$con = new PDO($dsn,'root','', $opt);

$sql = "SELECT date_created FROM tish_user";
$stm = $con->prepare($query);
$stm->execute();
while($row = $stm->fetch()) {
    echo $row['date_created'];
}
于 2013-02-16T12:53:48.117 回答