将mysql_fetch_assoc转换为PDO [英] Convert mysql_fetch_assoc to PDO

查看:53
本文介绍了将mysql_fetch_assoc转换为PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将以下内容安全地转换为PDO.有任何想法吗?感谢您的帮助.

I'm struggling to securely convert the below into PDO. Any ideas? Thanks for your help.

function getSlug($param)
{
    $query = mysql_query("SELECT * FROM articles WHERE slug = '$param'") OR die(mysql_error());
    return mysql_fetch_assoc($query);
}

推荐答案

PDO连接

连接和连接管理¶

通过创建PDO基础实例来建立连接 班级.您要使用哪个驱动程序无关紧要;你总是用 PDO类名称.构造函数接受用于指定的参数 数据库源(称为DSN)和用户名(可选) 和密码(如果有).

Connections are established by creating instances of the PDO base class. It doesn't matter which driver you want to use; you always use the PDO class name. The constructor accepts parameters for specifying the database source (known as the DSN) and optionally for the username and password (if any).

<?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 

}
?>

这篇关于将mysql_fetch_assoc转换为PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆