0
爲什麼這不起作用:PHP PDO準備語句:bindParam不起作用
public function query($query, $vars = array())
{
$link = $this->getLink();
if($link)
{
$stmt = $link->prepare($query);
if($stmt)
{
if(count($vars)>0)
{
$count = 1;
foreach($vars as $v)
{
$stmt->bindParam($count, $v);
$count++;
}
}
if($stmt->execute())
return $stmt->fetch(PDO::FETCH_ASSOC);
}
}
return false;
}
和這個作品:
public function query($query, $vars = array())
{
$link = $this->getLink();
if($link)
{
$stmt = $link->prepare($query);
if($stmt)
{
if($stmt->execute($vars))
return $stmt->fetch(PDO::FETCH_ASSOC);
}
}
return false;
}
電話:
$result = $db->query('select * from users where user like ? and email like ?',array('my_user', '[email protected]'));
與最終編輯代碼:
public function query($query, $vars = array())
{
$link = $this->getLink();
if($link)
{
$stmt = $link->prepare($query);
if($stmt)
{
if(count($vars)>0)
{
$count = 1;
foreach($vars as $v)
{
$stmt->bindValue($count, $v);
$count++;
}
}
if($stmt->execute())
return $stmt->fetch(PDO::FETCH_ASSOC);
}
}
return false;
}