所以我有一个应该处理所有数据执行操作的函数:sql
function loadResult($sql)
{
$this->connect();
$sth = mysql_query($sql);
$rows = array();
while($r = mysql_fetch_object($sth)) {$rows[] = $r;}
$this->disconnect();
return $rows;
}
我想将其转换为 pdo,这就是我目前所拥有的:pdo
function loadResult($sql)
{
$this->connect();
$sth = $this->con->prepare($sql);
//execute bind values here
$sth->execute();
$rows = array();
while ( $r = $sth->fetch(PDO::FETCH_OBJ) ) {$rows[] = $r;}
$this->disconnect();
return $rows;
}
下面是一个关于如何使用它来查看数据库中数据的函数示例:
function viewtodolist()
{
$db=$this->getDbo(); //connect to database
$sql="SELECT * FROM mcms_todolist_tasks";
//maybe the bind values are pushed into an array and sent to the function below together with the sql statement
$rows=$db->loadResult($sql);
foreach($rows as $row){echo $row->title; //echo some data here }
}
我刚刚提取了重要的片段,所以一些变量和方法来自其他 php 类。不知何故,mysql 查询工作正常,但 PDO 查询让我很头疼如何在viewtodolist()函数中包含 bindValue 参数以使其可重用。欢迎任何建议/建议。