获取mysql数据PDO

时间:2013-11-20 21:15:03

标签: php mysql pdo

我是PDO的新手我正试图从我的mysql表中获取数据我已经浏览了网络和其他stackoverflow的答案,但他们似乎都添加了额外的代码让我感到困惑。

我想回复一下我迄今为止得到的一张表中的所有数据:

<?php
$sth = $dbh->prepare("SELECT name, location, age FROM students");
$sth->execute();

$result = $sth->fetchAll(PDO::FETCH_CLASS, "students");
var_dump($result);
?>

连接信息

function getConnection()
{
    $DCONFIG_server = "localhost";
    $DCONFIG_DBuser = "root";
    $DCONFIG_DBpass = "root";
    $DCONFIG_dbname = "school";

    $conn = null;

    try
    {
        $conn = new PDO("mysql:host=$DCONFIG_server;dbname=$DCONFIG_dbname",$DCONFIG_DBuser, $DCONFIG_DBpass );
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }

    return $conn;
}

1 个答案:

答案 0 :(得分:0)

你有班级学生吗?

<?php
    class students {
        public $name;
        public $location;
        public $age;
    }
$sth = $dbh->prepare("SELECT name, location, age FROM students");
$sth->execute();

$result = $sth->fetchAll(PDO::FETCH_CLASS, "students");
var_dump($result);
?>