douyun6399 2015-04-07 18:17
浏览 63
已采纳

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

I have a interesting problem.

// in pdo with function --> not work

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;
}

// with MySQLi --> not working

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();
}

// In Register.php

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

So, with function not working, I try with:

$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();

but not working, the num_rows always return 0. And the account in the database successfuly created

  • 写回答

1条回答 默认 最新

  • dptdb84606 2015-04-07 18:22
    关注

    in pdo, num_rows won't work. you have to use $sql->rowCount() method to get number of records in a table.

    <?php
        $sql = $con->prepare("<YOUR SQL QUERY HERE>");
        $sql->execute();
        if($sql->rowCount() > 0){
            echo $sql->rowCount() ." rows found";
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了