Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用pdo类不会返回任何内容_Php_Mysql_Pdo - Fatal编程技术网

Php 使用pdo类不会返回任何内容

Php 使用pdo类不会返回任何内容,php,mysql,pdo,Php,Mysql,Pdo,我在网上上了一堂pdo课。现在,当我想在auth类中使用它时,我收到了任何结果。下面是我的pdo的一些功能 public function query($query){ $this->stmt = $this->dbh->prepare($query); } //binds the inputs with the placeholders we put in place public function bind($param, $value, $type = null

我在网上上了一堂pdo课。现在,当我想在auth类中使用它时,我收到了任何结果。下面是我的pdo的一些功能

public function query($query){
    $this->stmt = $this->dbh->prepare($query);
}

//binds the inputs with the placeholders we put in place
public function bind($param, $value, $type = null){
    if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }
        //$this->stmt->bindValue($param, $value, $type);
        $this->stmt->bindParam($param, $value, $type);
}

//executes the prepared statement
public function execute(){
    return $this->stmt->execute();
}

// returns an array of the result set rows
public function resultset(){
    $this->stmt->execute();
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

// returns a single record from the database
public function resultRow(){
    $this->stmt->execute();
    return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
下面是我如何在auth类的登录函数中使用它

public function login($user, $password){
//$pdo is a new of database class!
$pdo->query("SELECT user,pass FROM users WHERE user = ':user' , pass = ':pass'");     
$pdo->bind(':user', $user);
$pdo->bind(':pass', $password);
$result = $pdo->resultRow();
if($result == true) { //do sth
   return true;
}
else return false;
}
它返回错误!因为这是我第一次在php项目中使用pdo,所以我对使用它有点困惑


怎么了?

您的PDO语法没有问题,因此请检查$user和$password以及与DB的连接


另外,使用和代替coma会更好。

没有必要在占位符周围使用
'
。这就是占位符的全部要点。删除
'
周围的
:user
:pass
。您提供的SQL无效(此外,您正在使用
而不是
来连接
WHERE
语句的两部分)

您还可以通过将
PDO::ATTR_ERRMODE
设置为
PDO::ERRMODE_EXCEPTION
将PDO设置为更合适的调试模式。这使得调试任何SQL问题变得更容易

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
此外,我想指出,在PDO类中使用“current”语句并不是一个非常灵活的解决方案,它会导致开放数据库句柄泄漏。在实现
resultRow
的情况下,泄漏游标也是如此。对于一个小的web请求来说,这可能不是一个问题,但是如果您试图在一个更持久的应用程序中重用此代码,您将遇到问题


现在最好还是坚持使用标准的PDO。

您是否查看过PHP错误日志?如果在linux/apache服务器上,输入:sudo tail-f/path/to/logs,这些都在php.ini文件中设置。对不起,但是您真的需要在PDO之上有一个抽象层吗?我想我已经在这里看到了,他的示例的一个优点是您可以将方法链接在一起-您需要
返回$this
在bind语句的末尾允许这种情况发生(尽管我注意到您并没有在实现中这样做)@PatrickQ抽象层的概念就是这样-抽象。使用一种方法意味着当整个应用程序使用抽象层而不是特定的PDO查询时,可以更轻松地交换数据源。假设您必须更改为远程API数据源或SQLite或其他什么,编写新的抽象->数据库类比重写整个项目更容易。我做了更正。现在我得到了“致命错误:未捕获的异常‘PDOException’,消息为‘SQLSTATE[42000]”。我猜问题出在这行“$result=$pdo->resultRow();”上,因为这行后面的回显不起作用。但我不知道问题出在哪里!当然,这是目前的问题!!我不明白你所说的来自
resultRow
的问题是什么意思。php本身有我可以使用的类吗@MatsLindh@CodeLiker直接使用PDO,至少在您了解基本知识之前,可能会更好。SQLSTATE[42000]是一个通用的
语法错误
问题。如果您打印异常消息或转储异常本身,您应该会看到实际的错误(try/catch/var_dump)。我忙了一周来制作db和auth类(:羞耻),现在应该让它走了:)我只是担心我的教授不会接受它作为OO的东西。再试一次,然后你可能会接受你的建议。非常感谢。