0

I am trying to insert values into a MySQL database called 'todo' I made a PDO connection.

    <?php

 include_once('class.database.php');
 class ManageUsers{
     public $link;
     function __construct(){
         $db_connection = new dbConnection();
         $this->link = $db_connection->connect();
         return $this->link;
     }
     function registerUsers($username,$password,$ip_address,$time,$date){
         $query = $this->link->prepare("INSERT INTO users (username,password,ip_address,time,date) VALUES (?,?,?,?,?)");
         $values = array($username,$password,$ip_address,$time,$date);
         $query->execute($values);
         $counts = $query->rowCount();
         return $counts;
     }
 }
    $users = new ManageUsers();
    echo $users->registerUsers('bob','bob','127.0.0.1','12:00','29-02-2012');
?>

<?php

    class dbConnection{
            protected $db_conn;
            public $db_name = 'todo';
            public $db_user = 'root';
            public $db_pass = 'password';
            public $db_host = 'localhost';
            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();
                }
            }
    }
?>

The result should be 1 - i.e. it should add one row into the database. Instead, I get the following error message.

Fatal error: Uncaught Error: Call to a member function prepare() on string in C:\xampp\htdocs\classes\class.ManageUsers.php:15 Stack trace: #0 C:\xampp\htdocs\classes\class.ManageUsers.php(24): ManageUsers->registerUsers('bob', 'bob', '127.0.0.1', '12:00', '29-02-2012') #1 {main} thrown in C:\xampp\htdocs\classes\class.ManageUsers.php on line 15

What have I done wrong?

tereško
  • 58,060
  • 25
  • 98
  • 150
  • It's failing because your $link isn't working it looks like – clearshot66 Nov 29 '17 at 16:26
  • 2
    Why do you have a `return` in the constructor? – FirstOne Nov 29 '17 at 16:29
  • 1
    Since you're not checking what your `connect()` method actually returns (resource on success and a string if an exception occures), I would recommend not having that try/catch at all. If you're database connection doesn't work, there's usually no need to continue with the script anyway, at least if you need the DB for it to continue. – M. Eriksson Nov 29 '17 at 16:33

1 Answers1

0

First you should not return the $this->link in your ManageUsers::__construct. construct will by default return a new object of the class. Your error says,

Fatal error: Uncaught Error: Call to a member function prepare() on string in 

That means your $link property contains a string instead of a db object. Which makes sense because of the following portion of your code: (in dbConnection class)

catch(PDOException $e)
            {
                return $e->getMessage();
            }

once your database connection fails you are returning the error message using $e->getMessage() which returns a string.

In general if db connection fails you should terminate the script. If you change the script to something like:

catch(PDOException $e)
            {
                die($e->getMessage());
            }

You will know why your db connection is failing

Kamrul Khan
  • 3,260
  • 4
  • 32
  • 59
  • It says that it could not find the driver. I have already uncommented the line in php.ini, Do you know why? –  Nov 29 '17 at 16:41
  • did you restart your apache after you changed php.ini? – Kamrul Khan Nov 29 '17 at 16:45
  • Can I do that by simply hitting 'stop' and then 'start' in the xampp control panel? Because if so, yes, I have done that; it still says it cannot find the driver –  Nov 29 '17 at 16:55
  • yes that should work. check your phpinfo(). You should be able to see it from your xampp homepage. (or simply run echo phpinfo()). See if you can find `pdo_mysql` installed in there? – Kamrul Khan Nov 29 '17 at 17:09
  • It could not find that; what I did find is that PDO support is sqlite - is this the problem? Do I not have support for PDO? –  Nov 29 '17 at 17:12
  • Right, since you are using mysql, you do need pdo_mysql driver installed. Once you did that, you would see PDO support for both sqlite and mysql – Kamrul Khan Nov 29 '17 at 17:17