因为我在服务器上将PHP更新为PHP 5.5,所以我想用PDO而不是mysql_重写一个项目。 在这个项目中我有一个控制器和一个模型(当然也是一个视图,但这并不重要:-)) 在“旧”版本中,代码看起来像这样:
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版本中,它看起来像这样:
控制器(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显示:
捕获致命错误:第61行的/.../httpdoc/new/model/class.links.php中无法将类PDO的对象转换为字符串
第61行=公共函数updateLinks()
中的$sSql .= " $property='$value'";
我是PDO的新手,现在它完全是压倒性的。
有人可以给我一个正确的方向,如何解决这个问题。
非常感谢帮助。
提前致谢
标记
答案 0 :(得分:0)
我已使用此Answer 修改了您的代码。它使用延迟绑定。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");
}