【发布时间】:2013-07-25 11:39:20
【问题描述】:
我在 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;
}
}
【问题讨论】:
-
到底出了什么问题?
-
你为什么要做$sth->errorInfo();?干什么用的?
-
虽然很难相信所有这些语句都不起作用而最后一个语句起作用
-
别问我。程序员应该询问他们的代码。要么存在错误(必须显示),要么代码有效。就如此容易。你读过我提供的链接吗?