0

I'm trying to connect to a database by mysqli in an object oriented way. I had a few errors, and solved them, but now I just can solve this one. I've got my code here, and all the names (database name, user, password, host, and table names) are correct (actually, copied and pasted), but the query still returns 0.

<?php


class DbConnection
{
    public $link;

    public function __construct()
    {
        $this->link = new mysqli("localhost","root","","todo");
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
    }

    function RegisterUsers($username, $password, $ip, $name, $email)
    {
        $stmt = $this->link->prepare("INSERT INTO users (Username, `Password`, ip, Name, Email) VALUES (?,?,?,?)");
        $stmt->bind_param("sssss", $username, $password, $ip, $name, $email);
        $stmt->execute();

        $stmt->store_result();
        $count = $stmt->num_rows;
        return $count;
    }

}

$dbConn = new DbConnection();
echo $dbConn->RegisterUsers("a","a","a","a", "a");


?>

Edit: With this code, i get an

Call to a member function bind_param() on boolean

error.

This is my database

4
  • are you getting any errors? or use printf("Error: %s.\n", $stmt->error); to print errors Commented Jul 10, 2016 at 11:13
  • Yes, I got an error, didn't have the Email value. Now I get a Call to a member function bind_param() on boolean error. I updated the code @Naisapurushotham Commented Jul 10, 2016 at 11:16
  • add another placeholder to bind_param. It should look like this bind_param("sssss",$username, $password, $ip, $name, $email). One placeholder for each parameter, same for the ? markers, you need five of them Commented Jul 10, 2016 at 11:26
  • The ?, didn't remember them... That solved it! Thank you so much! @AlexAndrei Could you please post it on an answer? Commented Jul 10, 2016 at 11:30

1 Answer 1

1

Password and name are keywords in mysql. You have to put it in backticks to escape it, if you will use it as column name

    $stmt = $this->link->prepare("INSERT INTO users (Username, `Password`, ip, `Name`) VALUES (?,?,?,?)");
Sign up to request clarification or add additional context in comments.

4 Comments

@user6463207 check for errors after execute statement: php.net/manual/de/mysqli.error.php
Please, see my edit and comments on my post to solve my actual problem, thanks
@user6463207 now you have 5 column names and only 4 place holder in values. Please do not change the question, add all changes as updates to the question
Question changed, and code updated. Now I have the bind_param() with 5 string values and 5 variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.