pdo select和insert语句

时间:2012-12-22 13:05:24

标签: php sql

我几乎没有成功将我的经典db_connection转换为pdo,看起来似乎有效...... 但是有两个特定的select和insert语句有问题:

 $comments = array();
    $result = mysql_query("SELECT * FROM comments WHERE page='$_SESSION[page]' ORDER BY dt ASC");
    while($row = mysql_fetch_assoc($result))
    {
        $comments[] = new Comment($row);
    }
    foreach($comments as $c){
        echo $c->markup();
    } 

并且

mysql_query("INSERT INTO comments(page,name,url,email,body)VALUES (
'{$_SESSION['page']}',
'".$arr['name']."',
'".$arr['url']."',
'".$arr['email']."',
'".$arr['body']."'
)");

$arr['dt'] = date('r',time());
$arr['id'] = mysql_insert_id();  

这一切都按原样运作,但作为pdo - 我尝试了许多“例子” - 没有结果。

1 个答案:

答案 0 :(得分:1)

下面:

$comments = array();
$pdo = new PDO("mysql:host=localhost;dbname=db;",$username,$password);
$pdo->prepare("SELECT * FROM comments WHERE page='$_SESSION[page]' ORDER BY dt ASC");
$pdo->execute();
while($result = $pdo->fetch()) {
  array_push($comments,$result);
}

现在另一个人应该为你转变自己的蛋糕。