我在 StackOverFlow 上阅读了很多帖子,在 php dot net 上阅读了文档信息。
这是我正在尝试的代码:
示例 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;
}
}