我正在嘗試創建一個用戶管理系統類,而我的一個函數是檢查值是否在數據庫的列中。它什麼也沒有返回。OOP:檢查值是否在數據庫中的函數
以下是相關信息:
require("opperators/connect.php");
class UserManagemen
{
protected $db;
public function __construct(PDO $db)
{
$this->db = $db;
}
public function isInDatabase($checkIfThis, $isHere)
{
$stmt = $db->prepare("SELECT 1 from users WHERE $isHere = :$isHere");
$stmt->execute(array(':$isHere' => $checkIfThis));
if ($stmt->rowCount() > 0) {
echo "It's in the database";
} else {
echo "Not in the database and you are good to go!";
}
}
}
$usermanagement = new UserManagemen($db, null, null, null);
$usermanagement->isInDatabase(Batman, username);
在connect.php
:這個曾在我的程序編碼的測試。
$configurator['database'] = array(
'username' => 'root',
'password' => 'root',
'host' => 'localhost',
'db' => 'girlscouts',
);
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
);
try {
$db = new PDO("mysql:host={$configurator['database']['host']};dbname={$configurator['database']['db']};charset=utf8", $configurator['database']['username'], $configurator['database']['password'], $options);
}
catch (PDOException $ex) {
die("Failed to connect to the database: " . $ex->getMessage());
}
我提前道歉,如果這已被要求多次,我已經嘗試了谷歌,但什麼也沒有返回值,可能是因爲我不知道google一下。我在尋求幫助方面並不大,但我會給它一個機會。
你不需要WHERE子句中的'='嗎? – andrewsi
添加到anrewsi說,它看起來像PHP錯誤報告關閉,否則它會告訴你有一個錯誤在您的查詢。然後你可以使用php來找出問題所在,並得出與andrewsi相同的結論。 – Jasper
當然可以!修復它並測試它。還沒有改變最終結果,但它肯定是一個必要的變化,並證明我需要更多的咖啡。 –