我正在尝试创建一个用户管理系统类,我的功能之一是检查一个值是否在数据库的列中。它什么也不返回。
以下是相关信息:
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());
}
如果这被反复询问,我提前道歉,我已经尝试了谷歌但没有返回任何有价值的东西,可能是因为我不知道谷歌是什么。我不喜欢寻求帮助,但我会试一试。