我一直在閱讀很多帖子在StackOverFlow和PHP網點的文檔信息。PDOStatement :: bindParam && PDOStatement :: bindValue不工作
這裏是我tryng代碼:
例1
$id = 1;
$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = ?');
$sth->execute(array(intval($id)));
例2
$id = 1;
$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = :id');
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->execute();
例3
$id = 1;
$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = :id');
$sth->bindValue(':id', intval($id));
$sth->execute();
如果我試試這個:
$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = 1');
$sth->execute();
我得到我期望的結果,但它不是我要找的解決方案。
希望你能幫助我,在此先感謝。
//////////編輯1
在所有這樣的例子結束我這樣做:
$arr = $sth->errorInfo();
print_r($arr);
的回報是:
陣列([0] => 00000 [1] => [2] =>)
///////////編輯2
下面的代碼
class User {
private $registry;
private $userId;
private $userLan;
private $fullName;
private $dob;
private $email;
private $sex;
private $nationality;
private $valid; // User valid?
private $pdo; // PDO reference
/**
* Constructor del usuario. Se puede construir de dos formas, pasando email y password o pasando una id de usuario.
* @param Registry $registry.
* @param Int $id
* @param String $email
* @param String $password
* @param String $username
* @return void
*/
public function __construct (Registry $registry, $id, $email, $password, $username)
{
echo "constructor user with id = $id ";
$this->valid = false;
$this->registry = $registry;
if($id = 0 && $username != '' && $password != '')
{
// ...
$this->valid = true;
}
//else if($id > 0)
else
{
// $id = intval($id);
echo "second if";
$this->pdo = $registry->getObject('db')->getPdo();
try
{
$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = :id');
$sth->execute(array(':id' => intval($id)));
//$sth->execute(array(intval($id)));
$arr = $sth->errorInfo();
print_r($arr);
$result = $sth->fetch(PDO::FETCH_BOTH);
print_r($result);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
echo "passing query";
if($sth->rowCount() ==1)
echo "yeeeha";
else
echo "not yeeha (".$sth->rowCount().") ";
// ...
$this->valid = true;
}
}
到底哪裏出問題了呢? –
爲什麼你在做$ sth-> errorInfo();?做什麼的? –
問題是,前3個例子遵循正確的方式來編程,但沒有人工作。最後一次嘗試(在sql中使用1)正在工作,但無效。 – Jesus