我有一个返回用户ID的select语句:
$stmt = $dbh->prepare("SELECT user_id FROM users WHERE username = '" . $user_name . "'");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$user_id = $row['user_id'];
这工作正常,但当我调用一个应该返回朋友ID的函数时:(选择语句)
function get_friend($friend_username){
#create the PDO object
/**
* Used to instanciate the host of the server
* @var string
*/
$hostname = 'localhost';
/**
* Used to instanciate the username to connect to the server
* @var string
*/
$username = 'ODBC';
/**
* Used to instanciate the password to connect to the server
* @var string
*/
$pass = "";
/**
* Used to instanciate the database name
* @var string
*/
$db_name = 'bloggie_db';
try{
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name" , $username, $pass);
#set PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT user_id FROM users WHERE username = '" . $friend_username . "'");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$friend_id = $row['user_id'];
return $friend_id;
}catch(PDOException $e){
$e->getTrace();
}
$dbh = null;
} //The whole get friend function
函数调用:
$friend_id = $this->get_friend($friend_user_name);
如果我将$ friend_user_name替换为数据库中的值' testing@bloggie.com',则不返回任何内容,然后返回$ friend_id。为什么当我使用变量时它不会返回但是当我使用实际值时它会返回。
答案 0 :(得分:0)
我想出来了。通过锚标记将朋友ID发送到调用函数的页面完美地工作。谢谢你的帮助。