Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.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
致命错误:对中的布尔值调用成员函数rowCount()。第13行的loginc.php_Php_Pdo - Fatal编程技术网

致命错误:对中的布尔值调用成员函数rowCount()。第13行的loginc.php

致命错误:对中的布尔值调用成员函数rowCount()。第13行的loginc.php,php,pdo,Php,Pdo,我使用PDO登录页面 我无法登录,因为它显示了致命错误:调用布尔值中的成员函数rowCount。第13行的loginc.php 我的连接页面:config.php <?php $host = '127.0.0.1'; $db = 'pan'; $user = 'root'; $pass = ''; $charset = 'utf8mb4'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $options = [ PDO

我使用PDO登录页面

我无法登录,因为它显示了致命错误:调用布尔值中的成员函数rowCount。第13行的loginc.php

我的连接页面:config.php

 <?php
$host = '127.0.0.1';
$db   = 'pan';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
 $pdo = new PDO($dsn, $user, $pass, $options);

} catch (\PDOException $e) {
 throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
我的loginc.php页面如下所示:

<?php
session_start();
include('../../config/config.php');
extract($_POST);



        $un=$_POST['un'];
        $pw=$_POST['pw'];



        $q=$pdo->prepare("select * from `admin` where userid=? and pass=?")->execute([$un,$pw])->rowCount();


        if($q==1)
        {
        $_SESSION['login_as']="admin";
        header("location:home_page.php");
        }
        else
        {
        $_SESSION['e']="Sorry...! Your username or password is incorrect.";
        header('location:../index.php');
        }


?>
我无法理解错误消息的错误所在。

基于PHP文档,最好使用
$q = $pdo->prepare("select * from `admin` where userid= ? and pass= ? ");
$q->execute(array($un,$pw));
$q->rowCount();
查询和获取列,因为:

对于大多数数据库,PDOStatement::rowCount不会返回受SELECT语句影响的行数

这是PHP文档中的一个示例:

<?php
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {

    /* Check the number of rows that match the SELECT statement */
    if ($res->fetchColumn() > 0) {

        /* Issue the real SELECT statement and work with the results */
        $sql = "SELECT name FROM fruit WHERE calories > 100";

        foreach ($conn->query($sql) as $row) {
            print "Name: " .  $row['NAME'] . "\n";
        }
    }
    /* No rows matched -- do something else */
    else {
        print "No rows matched the query.";
    }
}

$res = null;
$conn = null;
?>
希望这有帮助

的结果是布尔值。 错误的原因是$pdo->prepareselect*fromadminwhere userid=?pass=?->execute[$un,$pw]返回布尔值,但您尝试 调用此布尔值的行计数

尝试下一个代码:

<?php
    session_start();
    include('../../config/config.php');
    extract($_POST);

    $un = $_POST['un'];
    $pw = $_POST['pw'];

    try {
        $stmt = $pdo->prepare("select * from `admin` where userid=? and pass=?");
        $stmt->execute([$un, $pw]);
        /* Or replace $stmt->execute([$un, $pw]); with next lines:
        $stmt->bindParam(1, $un);
        $stmt->bindParam(2, $pw);
        $stmt->execute();
        */
        $q = $stmt->rowCount();

        if ($q == 1) {
            $_SESSION['login_as']="admin";
            header("location:home_page.php");
        } else {
            $_SESSION['e']="Sorry...! Your username or password is incorrect.";
            header('location:../index.php');
        }
    } catch (\PDOException $e) {
        throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }   
?>