PHP选择语句 - 错误

时间:2015-12-20 15:30:16

标签: php html mysqli

我是学生,我在noteapp上工作。我在登录掩码中的DB-Handler中创建了2个方法,供用户登录或注册。

    <?php


    class DBHandler
    {

var $hostname;
var $user;
var $pw;
var $db;

var $connection;

function connectToDB($hostname,$user,$pw,$db){
    $this->hostname = $hostname;
    $this->user = $user;
    $this->pw = $pw;
    $this->db = $db;

    $this->connection = new mysqli($this->hostname,$this->user,$this->pw,$this->db);

    if ($this->connection->connect_error) {
        die('Failed to connect' . $this->connection->connect_error);
    }

    $this->ensureNotesTable();
    $this->ensureUsersTable();
}

function ensureUsersTable(){
    assert($this->connection);

    $queryCreate = "CREATE TABLE IF NOT EXISTS users(id INT(5) PRIMARY KEY AUTO_INCREMENT, username VARCHAR(100) NOT NULL, password VARCHAR(100) NOT NULL)";
    $this->connection->query($queryCreate);
}

function ensureNotesTable(){
    assert($this->connection);

    $queryCreate = "CREATE TABLE IF NOT EXISTS notesnew(id INT(5) PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100) NOT NULL, content VARCHAR(100) NOT NULL, userid INT(5) NOT NULL)";
    $this->connection->query($queryCreate);
}

function ensureUsername($username,$password){
    assert($this->connection);

    echo $username;

    $query = "SELECT * FROM users WHERE username = (?)";
    $statement = $this->connection->prepare($query);
    $statement->bind_param('s',$username);
    $results = $statement->execute();
    echo $this->connection->errno.'-'.$this->connection->error;

    if(mysqli_num_rows($results) >= 1){
        echo $this->connection->errno.'-'.$this->connection->error;
        echo "Username already exists";
    } echo "Username is free!";
        $this->addUser($username,$password);
}


function addUser($username,$password){
    assert($this->connection);

    $queryCreate = "INSERT INTO users(username, password) VALUES (?,?)";
    $statement = $this->connection->prepare($queryCreate);
    $statement->bind_param('ss', $username, $password);

    return $statement->execute();

    echo "You have been registered!";
}

function getUserId($username){
    assert($this->connection);

    $queryCreate = "SELECT id FROM users WHERE username = $username";
    $row = $this->connection->query($queryCreate);

    $userid = mysqli_fetch_row($row);
    return $userid[0];
}

function addNote($title, $text, $userID){
    assert($this->connection);

    $queryCreate = "INSERT INTO notesnew(title, content, userid) VALUES (?,?,?)";
    $statement = $this->connection->prepare($queryCreate);
    $statement->bind_param('ssi', $title,$text,$userID);

    return $statement->execute();
}

}

在ensureUsername中,我想检查用于注册的用户名是否已被其他用户选中。

在addUser中我想做Insert语句,如果用户名是免费的,则将用户添加到数据库。

我今天尝试了大约3个小时,但它总是给我错误。我讨厌它!也许我对它太愚蠢了。

目前它的说法是:

  

警告:mysqli :: query()期望参数1为str​​ing,object   给定的   C:\ Users \ ReallySorry \ PhpstormProjects \ NoteAppMongo \ DBHandler.php on   第57行0-警告:mysqli_num_rows()期望参数1为   mysqli_result,null中给出   C:\ Users \ ReallySorry \ PhpstormProjects \ NoteAppMongo \ DBHandler.php on   第60行

有人知道我做错了吗?

谢谢...

郁闷的学生

2 个答案:

答案 0 :(得分:3)

你的问题在这里:

$statement = $this->connection->prepare($query);
$statement->bind_param('s',$username);
$results = $this->connection->query($statement)

当你使用准备好的语句时(做得好 - 这里很多人都没有!)你需要在语句中使用execute()方法,而不是在你的连接上调用query()。所以,这应该有效:

$results = $statement->execute()

答案 1 :(得分:1)

原始版本的一些细微变化 -

function ensureUsername( $username=false, $password=false ){
    $rv=false;/* Return Value */
    if( !assert( $this->connection ) or !$username or !$password ) return $rv;
    $db=$this->connection;/* shorthand for laziness */


    $sql = "select `username` from `users` where `username`=?;";
    $stmt = $db->prepare( $sql );
    $stmt->bind_param('s', $username );
    $res = $stmt->execute();
    $stmt->store_result();

    if( $res ){
        $rv=( $stmt->num_rows > 0 ) ? 'Sorry, that Username already exists!' : $this->addUser( $username, $password );
    }
    $stmt->free_result();
    $stmt->close();
    echo $rv;
}


function addUser( $username, $password ){
    $db=$this->connection;/* No need for assert now, if the script gets here the db conn must exist */

    $sql = "insert into `users` (`username`, `password`) values (?,?);";
    $stmt = $db->prepare( $sql );

    $stmt->bind_param('ss', $username, $password );

    return $stmt->execute() ? 'You have been registered!' : 'Sorry, there was a problem';
}