[英]PDO:: What's wrong with this query?
我班上的方法能正常工作。 不要給我一些錯誤信息,但是根本行不通。
public function query($value)
{
$this->__error = FALSE;
$sql = "SELECT * FROM users WHERE username = ".Input::input($value);
if ($this->__query = $this->__pdo->query($sql))
{
$this->__result = $this->__query->fetchAll(PDO::FETCH_OBJ);
$this->__count = $this->__query->rowCount(); //Here is the problem
}
else {
$this->__error = TRUE;
}
return $this;
}
public function count()
{
return $this->__count;
}
但是我不會編寫整個類的代碼,我提到正確定義了PDO DataBase連接( $ _ pdo property ),還有負責與數據庫通信的實例。 ( $ _instance屬性 )。 也輸入類。
這是我的index.php(某種注冊表格):
<?php
spl_autoload_register(function($class) //Load all class in project
{
require_once 'class/'.$class.'.php';
}
);
$user = DataBase_class::instance()->query("username"); //username is the name of textbox
if ($user->count())
{
echo 'User exist';
}
else echo 'User not exist';
?>
結果是“用戶不存在”,盡管用戶存在100%。
你忘了報價
$sql = "SELECT * FROM users WHERE username = '".Input::input($value) . "'";
但您應該考慮使用准備好的語句。
$stmt = $this->__pdo->prepare("SELECT * FROM users WHERE username = :name");
$stmt->bindParam(':name', Input::input($value));
$result = $stmt->execute();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.