Potential logical error PHP PDO

I am new to PHP and would like to know why this code does not insert anything into the database (returns 0). I am sure that this should be a logical error, since I do not receive error messages.

class DbConnection {
    protected $db_conn;
    public $db_host = "localhost";
    public $db_name = "todo";
    public $db_user = "root";
    public $db_pass = "";

    function connect () {
        try {
            $this->db_conn = new PDO ("mysql: host = $this->db_host; dbname = $this->db_name", $this->db_user, $this->db_pass);
            return $this->db_conn;
        }
        catch (PDOException $e) {
            return $e->getMessage ();
        }
    }
}

class ManageUsers {
    public $link;

    function __construct () {
        $db_connecton = new DbConnection ();
        $this->link = $db_connecton->connect ();
        return $this->link;
    }

    function registerUsers ($username, $password, $ip_address, $reg_time, $reg_date) {
        $query = $this->link->prepare("INSERT INTO users (username, password, ip_address, reg_time, reg_date) VALUES (?,?,?,?,?)");
        $values = array ($username, $password, $ip_address, $reg_time, $reg_date);
        $query->execute ($values);
        $counts = $query->rowCount ();
        return $counts;
    }

}

$users = new ManageUsers ();
echo $users->registerUsers ("bob", "lassie", "127.0.0.2", "13:37", "03-14-15");
+4
source share
1 answer

You should not put spaces in your PDOconnection, otherwise it will take strange things, for example. he thinks you are the name db: [space]todoetc., so just remove all spaces in the connection string, for example:

$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass);
                             //^     ^              ^      ^no spaces
+2
source

All Articles