准备好的陈述和选择,找到0行

时间:2016-08-16 12:35:16

标签: php prepared-statement

好吧所以我的问题很简单,我试图从查询中获得结果,但它总是返回没有用户注册的用户。

<?php 
    require "assembly/oop/sql_classbuild.php";

    class player {
        private $name;
        private $age;
        private $gender;
        private $location;

        public function createNew($name, $age, $gender, $location) {

            $conni = new sql();
            $conn = $conni->connect();

            $stmt = $conn->prepare("INSERT INTO players (name, age, gender, location) VALUES (?,?,?,?)");
            $stmt->bind_param("siss", $name, $age, $gender, $location);

            $this->name = $name;
            $this->age = $age; 
            $this->gender = $gender;
            $this->location = $location;

            $stmt->execute();

            $stmt->close();
            $conn->close();
        }

        public function isRegistered($name) { //problematic code

            $conni = new sql();
            $conn = $conni->connect();

            $stmt = $conn->prepare("SELECT name FROM players WHERE name=?");
            $stmt->bind_param("s", $name);

            $stmt->execute();

            if($stmt->num_rows >= 1) { //if there is a registered member
                $stmt->close();
                $conn->close();
                return true;
            }
            else {
                $stmt->close();
                $conn->close();
                return false;
            }
        }

    }



?>

现在我也尝试打印num_rows,但它只返回0,就像数据库中没有结果一样。 用法非常简单

if($player->isRegistered("Test") == true) {
        echo "Hello";
    }
    else {
        echo "Non-existing user";
    }

SQL截图 http://imgur.com/a/TXqiJ 这也是sql类pastebin.com/Evg13CUc

1 个答案:

答案 0 :(得分:0)

问题解决了。 我用$ stmt-&gt; store_result();它以某种方式解决了,它给了我很好的结果。