如何使用PDO从数据库中获取内容?

时间:2014-02-04 14:52:25

标签: php pdo

我是PHP的新手,只是在学习。

 <?php
 // database connection
 $dbhost     = "localhost";
 $dbname     = "pdo";
 $dbuser     = "root";
 $dbpass     = "7777777";

 $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass)


 // echo from database
$select = $conn->query('SELECT username FROM users');
while($row = $select->fetch(PDO::FETCH_ASSOC))
{
    $username = $_GET['username'];
    echo $username;
}

 ?>

这会出错:

  

解析错误:语法错误,意外的T_PUBLIC在行:$ select = $ conn-&gt; query('SELECT username FROM users');

1 个答案:

答案 0 :(得分:2)

需要更改连接

From:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass)

To:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

连接缺少;