mysql_ -> PDO |我怎样才能达到相同的结果

标签 mysql class pdo php-5.5

自从我在服务器上将 PHP 更新到 PHP 5.5 后,我想使用 PDO 而不是 mysql_ 重写一个项目。 在这个项目中,我有一个 Controller 和一个模型(当然还有一个 View ,但这并不重要:-)) 在“旧”版本中,代码如下所示:

Controller (mysql_)

public function saveAction()
{
    $iFilterID = filter_input(INPUT_POST, 'id');
    $this->_setRealEscape($iFilterID);
    $iReqID = $this->_getRealEscape(); 
    if (isset($_REQUEST['aLinks']))
    {
        $this->oSubCat = new links ();
        if (isset($iFilterID))
        {
            $this->oSubCat->loadLinksID($iReqID);
        }
        foreach ($_REQUEST['aLinks'] AS $key => $value)
        {
            $value = $this->_cleanString($value);

            $this->oSubCat->$key = $value;
        }
    }
    $this->oSubCat->saveLinks();


}

模型(mysql_)

public function loadLinksID($id)
{
    $sSql = "SELECT * FROM links WHERE id =".$id;
    $query = mysql_query($sSql);
    $oLinks = mysql_fetch_object($query);
    if(is_object($oLinks))
    {
        foreach ($oLinks as $key => $value)
        {
            $this->$key = $value;
        }
    }

}

public function saveLinks ()
{
    if ($this->id)
    {

        $this->updateLinks();
    }
    else 
    {
        $this->insertLinks();
    }
}

public function updateLinks ()
{
    $sSql = "UPDATE links SET";
    $first = true;
    foreach ($this as $property => $value) {
        if ($first) {
            $first = false;
            $sSql .= " $property='$value'";
        } else {
            $sSql .= ", $property='$value'";
        }
    }
    $sSql .= " WHERE id = ".$this->id;
    mysql_query($sSql);
    header("location: index.php?module=helper&action=success&func=links");
}

在 PDO 版本中,它看起来像这样:

Controller (PDO)

public function saveAction()
{
    $iFilterID = filter_input(INPUT_POST, 'id');
    if (isset($_REQUEST['aLinks']))
    {
        $this->oSubCat = new links ();
        if (isset($iFilterID))
        {
            $this->oSubCat->loadLinksID($iFilterID);
        }
        foreach ($_REQUEST['aLinks'] AS $key => $value)
        {
            $this->oSubCat->$key = $value;
        }
    }
    $this->oSubCat->saveLinks();


}

模型(PDO)

 public function loadLinksID($id)
{
    $PDOpre = $this->connection->prepare("SELECT * FROM links WHERE id =:id");
    $PDOpre->bindvalue(':id',$id, PDO::PARAM_STR);
    $PDOpre->execute();
    $oLinks = $PDOpre->fetch(PDO::FETCH_OBJ);

    if(is_object($oLinks))
    {
        foreach ($oLinks as $key => $value)
        {
            $this->$key = $value;
        }
    }

}

public function saveLinks ()
{
    if ($this->id)
    {

        $this->updateLinks();
    }
    else 
    {
        $this->insertLinks();
    }
}

public function updateLinks ()
{
    $sSql = "UPDATE links SET";
    $first = true;
    foreach ($this as $property => $value) {
        if ($first) {
            $first = false;
            $sSql .= " $property='$value'";
        } else {
            $sSql .= ", $property='$value'";
        }
    }
    $sSql .= " WHERE id = ".$this->id;
    echo $sSql;
    $this->connection->query($sSql);
    header("location: index.php?module=helper&action=success&func=links");
}

mysql_-Version 工作正常,但 PDO-Version 显示:

Catchable fatal error: Object of class PDO could not be converted to string in /.../httpdoc/new/model/class.links.php on line 61

第 61 行 = $sSql .= "$property='$value'"; 公共(public)函数 updateLinks()

我对 PDO 完全陌生,现在它完全让人不知所措。

有人可以给我一个正确的方向,如何解决这个问题。

非常感谢您的帮助。

提前致谢

标记

最佳答案

我已经使用此 Answer 修改了您的代码。它使用惰性绑定(bind)。 PDO Info

public function updateLinks ()
{
    $query_params = array();Array to hold parameters for lazy binding
    $sSql = "UPDATE links SET";
    $first = true;
    foreach ($this as $property => $value) {
        if ($first) {
            $sSql .= " $property=?";
            $first = false;//Set after first

        } else {
            $sSql .= ", $property =?";
        }
    array_push($query_params,$value);//Push values into array
    }
    array_push($query_params,$this->id);
    $sSql .= " WHERE id = ?";
    echo $sSql;
    $stmt = $this->connection->prepare($sSql);
    $result = $stmt->execute($query_params);

    header("location: index.php?module=helper&action=success&func=links");
}

关于mysql_ -> PDO |我怎样才能达到相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28169705/

相关文章:

mySql 存储过程加密

Java 线程 - CPU 使用率

c# - 无法从 C# WPF 中的另一个窗口调用方法

php - 更新单个特定 MySQL 单元不起作用

php - 在执行 PDO::exec() 时调用非对象上的成员函数 exec()

php - PHP 的 CRUD 类(简单但允许关系数据)

mysql - 如何使用 MySQL Workbench 创建函数?

javascript - 使用 PDO 查询构建选择列表作为 optional

mysql - MySQL 中 VARCHAR 的 Between 函数

java - 将 Java applet 项目导入 netbeans 将无法正常工作,即使手动设置,Netbeans 也拒绝识别甚至找不到主类