php - num_rows 不工作 pdo,mysqli,总是返回 0

标签 php mysql pdo mysqli

我有一个有趣的问题。

//在具有功能的 pdo 中 --> 不起作用

function UserIsExist($name)
{
    global $db;
    $stmt = $db->prepare("SELECT id FROM tarskereso_users WHERE email = '$name' LIMIT 1");
        $stmt->execute();
        if ($stmt->fetchColumn() == 1) return 1;
        else return 0;
}

//MySQLi --> 不工作

function UserIsExist($name)
{
    global $db;
    $stmt = $db->prepare("SELECT id,email FROM tarskereso_users WHERE email = ? LIMIT 1");
        $stmt->bind_param('s', $name);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows == 1) 
            return 1;
        else 
            return 0;
        $stmt->close();
}

//在 Register.php 中

 ... other ..
        if(UserIsExist($user) == 1)
            $error_msg = "Is Exist";
        else
        {
            $birthdate = $year.'.'.$month.'.'.$day;
            CreateUser($user,$pass,$birthdate,$sex);
                $error_msg = 'Success';
        }

因此,由于功能不起作用,我尝试:

$stmt = $db->prepare("SELECT id,email FROM tarskereso_users WHERE email = ? LIMIT 1");
        $stmt->bind_param('s', $name);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows > 0) 
            ... other ...
        else 
            echo 'isnt exist...';
        $stmt->close();

但不工作,num_rows 总是返回 0。并且数据库中的帐户已成功创建

最佳答案

在 pdo 中,num_rows 不起作用。 你必须使用 $sql->rowCount() 方法来获取表中的记录数。

<?php
    $sql = $con->prepare("<YOUR SQL QUERY HERE>");
    $sql->execute();
    if($sql->rowCount() > 0){
        echo $sql->rowCount() ." rows found";
    }
?>

关于php - num_rows 不工作 pdo,mysqli,总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498406/

相关文章:

MySQL - UNION ALL 与同一张表上的查询

Mysql order_id错误

php - 我想使用 PHP 的 PDO 将数据插入到 mysql 数据库中。但是没有插入数据

php - MySql 查询在 PhpMyAdmin 中耗时 0.16 秒,但在 Php 中耗时 10 分钟

php - $_SERVER ['HTTP_REFERER' ] 安全吗?

PHP 函数在数据库中搜索值并在循环中替换

mysql - 如何在 Java 应用程序中使用 Hibernate 和 MySQL 清除或删除数据库

PHP PDO 更新 - 需要参数和无效的参数号

javascript - Google reCAPTCHA 重置由函数调用呈现的多个验证码

php - Wamp vhost docroot 始终显示为 c :\wamp\www not the vhost docroot