Q:

mysql_ -> PDO | How can I achieve the same result

since I update PHP to PHP 5.5 on my server, I want to rewrite one project using PDO instead of mysql_.
In this project I have a controller and a modell (naturally also a view, but that's not important :-) )
In the "old" version the code lookes like this:
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();

}

model (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");
}

In the PDO-Version it looks like this:
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();

}

model (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");
}

The mysql_-Version works fine but the PDO-Version shows:

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

Line 61 = $sSql .= " $property='$value'"; in public function updateLinks()
I am totally new to PDO and right now it's completely overwhelming. 
Can someone give me a shove in the right direction, how to solve this.
Help is really appreciated. 
Thanks in advance
Mark

A:

I have modified your code using this Answer . It uses lazy binding.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");
}

