所以我的情况是这样的,我尝试根据isset正在获取的id
来获取用户详细信息,但有些问题是variable that contains the $_GET value doesn't work
在查询中但是当我放置静态时值为pdo查询然后它工作并显示结果。我在查询之前通过变量var_dump
进行了$user
检查,它显示了正确的值,但未在查询中工作。以下是我正在使用的代码:
public function profile_view($user_id = null) {
$user = $user_id;
$stmt = $this->_db->prepare('SELECT memberID,username,email,profile_pic,active FROM members WHERE memberID = :user_id AND active="YES"');
$stmt->execute(array(':user_id'=>$user));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$user_det = (object) array('username'=> $row['username'],'email'=>$row['email'],'profile_pic'=>$row['profile_pic'],'id'=> $row['memberID'],'active'=>$row['active']);
return $user_det;
}
}
这是调用函数的方式(profile_view函数是User类的子类,所以$ user是类User):
$view_profile = $user->profile_view($_GET['u']);
上面的代码返回null但是当我在5
的{{1}}处放置静态值:$user
时,它会返回整个用户的详细信息,这就是我需要的,但它没有使用令我困惑的变量。
答案 0 :(得分:0)
你纠正了PHP代码。 SQL查询false(可能)。我不认识你的SQL表。
示例php代码手册。此链接http://php.net/manual/en/pdostatement.execute.php
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>
您编写SQL代码并测试代码。
答案 1 :(得分:-3)
当你调用该函数时,无论是通过它传递的是什么,你都要将$user_id
设置为null,而不是取$user_id
的值。试试这个:
$user_id = 5; //Set this to whatever int you want whether it be via POST or GET
public function profile_view($user_id) { //Note that I don't set $user_id equal to anything
//The rest of the code remains the same
$user = $user_id;
$stmt = $this->_db->prepare('SELECT memberID,username,email,profile_pic,active FROM members WHERE memberID = :user_id AND active="YES"');
$stmt->execute(array(':user_id'=>$user));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$user_det = (object) array('username'=> $row['username'],'email'=>$row['email'],'profile_pic'=>$row['profile_pic'],'id'=> $row['memberID'],'active'=>$row['active']);
return $user_det;
}
}