我正在努力将下面的内容安全地转换为PDO。有任何想法吗?谢谢你的帮助。
function getSlug($param)
{
$query = mysql_query("SELECT * FROM articles WHERE slug = '$param'") OR die(mysql_error());
return mysql_fetch_assoc($query);
}
答案 0 :(得分:2)
连接和连接管理¶
通过创建PDO基础的实例来建立连接 类。你想要使用哪个驱动程序并不重要;你经常使用 PDO类名称。构造函数接受用于指定的参数 数据库源(称为DSN)和可选的用户名 和密码(如果有的话)。
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); <<== This is the PDO DATABASE OBJECT
function getSlug($param)
{
$sth = $dbh->prepare("SELECT * FROM articles WHERE slug = ?"); <<== First you need to prepare it
$sth->execute(array($param)); <<== Then execute it using params
$result = $sth->fetchAll(PDO::FETCH_ASSOC); <<== Then USe PDO Constant to get Associative array
return $result; <<<== Then return it
}
?>