Php PDO print_r()返回1
因此,我有一个PDO和MySQL脚本,用于根据用户的用户名或屏幕名检索结果,在本例中是e 首先,我在文件开头有一个函数,用于连接数据库。它存在于functions.php文件中,并且在每个页面的开头都是必需的,因此全球化。据我所知,这个函数没有任何问题Php PDO print_r()返回1,php,mysql,database,pdo,Php,Mysql,Database,Pdo,因此,我有一个PDO和MySQL脚本,用于根据用户的用户名或屏幕名检索结果,在本例中是e 首先,我在文件开头有一个函数,用于连接数据库。它存在于functions.php文件中,并且在每个页面的开头都是必需的,因此全球化。据我所知,这个函数没有任何问题 function SQLConnect () { // Database connection variables $host = "localhost"; $dbname = "dropbox"; $user = "root"; $passwo
function SQLConnect () {
// Database connection variables
$host = "localhost";
$dbname = "dropbox";
$user = "root";
$password = "ethan17458";
// Connect to the database
try {
//put $connect in global scale of document
global $connect;
// attempt to connect to database
$connect = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
// Sets error mode
$connect->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
// Retrieves error message if connection fails
echo $e->getMessage();
}
}
此函数使用PDO连接到包含用户信息的数据库
接下来是检索用户数据的脚本
// Test user in database
$test = "e";
try {
//confirms running of "try" block
echo "tried";
//database information
$host = "localhost";
$dbname = "dropbox";
$user = "root";
$password = "ethan17458";
//Prepare statement from connection function
// username_raw is "e"
//username should be e1671797c52e15f763380b45e841ec32 (md5)
$statement = $connect->prepare("SELECT `username` FROM `users` WHERE `username_raw` = ':name'");
//create placeholder for prepared statement
$statement->bindParam(":name", $test);
//make the statement fetch in an associative array
$statement->setFetchMode(PDO::FETCH_ASSOC);
//execute the prepared statement
$statement->execute();
//set $get_result to the fetched statement
$get_result = $statement->fetch();
//attempt to display the data fetched in $get_result
echo "<br />";
echo "<pre>";
//Outputs 1 for some reason
// **not working**
echo print_r($get_result);
echo "</pre>";
echo "<br />";
} catch (PDOException $e) {
//confirm running of "catch" block
echo "caught";
// echo error message
echo $e->getMessage();
}
在这个输出中,try是对try语句已被处理的确认,而1是我开始遇到问题的地方
如果脚本按照我的意愿工作,脚本将从数据库检索数据e1671797c52e15f763380b45e841ec32,因为它是列用户名,其中用户名_raw是e,如PDO prepared语句中所述
理想的输出应该是
我做错了什么?fetch返回false,不会在屏幕上打印任何内容。这是错误的,因为您没有得到任何结果,因为您在查询中的参数周围加了单引号,PDO会为您处理。只需删除:name.只需打印,无回显。这不会输出任何内容。打印将显示请求的输出并返回布尔值true,然后您将回显该值。我如何显示请求的输出@MarkBaker?打印应始终显示输出,在本例中,值为$get\u result,即使输出为空,我也会在有机会时进行确认,并选择答案(如果有效)。谢谢
tried
1
tried
e1671797c52e15f763380b45e841ec32