因此,我有一個PDO和MySQL腳本,用於根據用戶的用戶名或屏幕名稱檢索結果,在本例中爲e
。PDO print_r()返回1
首先,我在用於連接數據庫的文件的開頭有一個函數。 (它存在於每個頁面開頭的functions.php
文件和required
,因此是全球化)。這個函數沒有任何錯誤(據我所知)。
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();
}
當我運行該腳本,我得到這樣的輸出:
tried
1
在此輸出,tried
是確認了「嘗試」聲明已處理,並且1
是我開始遇到問題的地方。
如果腳本工作,我想,該腳本會從數據庫中檢索數據e1671797c52e15f763380b45e841ec32
,因爲它是列username
其中username_raw
是e
,如規定在PDO預處理語句。
理想的輸出應
tried
e1671797c52e15f763380b45e841ec32
我在做什麼錯?
只是print_r,沒有回聲。不輸出任何東西的 –
。 – ethan17458
'print_r()'顯示請求的輸出__and__返回一個布爾值true,然後回顯 –