Hullo,

I have decided to move away from procedural PHP onto OOPHP. I figured I would make a simple PHP connect, if no database found create it script but can't seem to get it to work. Here is the simple code I have created:

<?php
   class DBConfig {
      private $db_username = 'laura';
      private $db_password = 'blackman';
      private $db_server = 'localhost';
      private $db_name = 'laura_blackman_creative';
      private $connection;

  public function __construct() {
     $this->connection = mysqli_connect($this->db_server, $this->db_username, $this->db_password) or die('Could not connect to database');
     if(!mysqli_select_db($this->db_name, $this->connection)) {
        echo 'creating database <br />';
        $this->createDB();
     }
     if(!mysqli_select_db($this->db_name, $this->connection))
        echo 'database not created';
     else
        echo 'database created';

  }

  private function createDB() {
     $create_db = "CREATE DATABASE ".$this->db_name;
     return mysqli_query($create_db, $this->connection);

     mysqli_select_db($this->db_name, $this->connection) or die('Database '.$db_name.' not created');

     $create_table_member = "CREATE TABLE member (
                    id int NOT NULL auto_increment PRIMARY KEY,
                    user_name varchar(20) NOT NULL default '' UNIQUE,
                    password char(32) binary NOT NULL default '',
                    cookie char(32) binary NOT NULL default '',
                    session char(32) binary NOT NULL default '',
                    ip varchar(15) binary NOT NULL default '',
                    administrator tinyint(1) NOT NULL
                    )";
     return mysqli_query($create_table_member, $this->connection);
  }
   }

   $link = new DBConfig();
?>

Currently I am just calling this from the browser window on my localhost test site. I checked that I was able to get a proper connection to the database and had the privileges required to create databases the first time it wasn't working with the procedural approach which worked as expected. (If you would like me to post the procedural approach I coded than let me know but it seemed redundant.)

I have built this, and rebuilt this enough times that the code looks correct to me but I still can't seem to find why it isn't working. When I run the code I get this printed back to me:

creating database
database not created

So I know it must be the method createDB() but to me it all looks correct, and from what I can tell on php.net it should work. I am sure that I have missed something small about OOPHP that is the reason this isn't working, but I cannot figure it out. I would greatly appreciate someone who is a better coder than I to point me to my foolish ways.

I have also checked the error log just in case and as would be expected there is no errors. A couple of warnings:

[Sun Jan 15 14:59:40 2012] [error] [client 127.0.0.1] PHP Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /var/www/laura/include/connect.php on line 11
[Sun Jan 15 14:59:40 2012] [error] [client 127.0.0.1] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /var/www/laura/include/connect.php on line 24
[Sun Jan 15 14:59:40 2012] [error] [client 127.0.0.1] PHP Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /var/www/laura/include/connect.php on line 15

but they shouldn't be preventing this.

Thanks in advance,
Duncan

    mysqli is reverse of mysql... IE mysql_query($query,$conn) mysqli_query($conn,$query). Also why make a class for the DB configs then use all the procedural versions of mysqli instead of the mysqli CLASS?

      Wow thanks for the quick response, as I had mentioned I am transferring over from procedural - which doesn't mean I want to write sloppy OOPHP, just the reason for the error.

      I did a few weeks of research and then wrote the program, however I am not sure I understand what you mean still, I am not sure what you mean by:

      Derokorian;10994569 wrote:

      Also why make a class for the DB configs then use all the procedural versions of mysqli instead of the mysqli CLASS?

      Or I guess I should properly say that I didn't realize mysqli was a class of its own (php.net isn't the clearest resource.) Thanks for the heads up and I will look into the mysqli class. If you have any other advice, or know of other reading material that will ease the transition I am making that would also be greatly appreciated.

      Thanks again,
      Duncan

        Hey thanks again for the help Derokorian. After tinkering around on php.net, and with my code I believe I have a proper OOPHP basis to continue from.

        If anyone is searching for a similar solution I thought I would post what I have - and if anyone happens to see a fault with what I did let me know. You will also notice I corrected a rookie OOP mistake from the first posted code, which is simply put you can't have two return statements in the same method if you want the second return statement to be read as well.

        <?php
        	class DBConfig {
        		private $db_username = 'user';
        		private $db_password = 'password';
        		private $db_server = 'localhost';
        		private $db_name = 'testDB';
        		private $connection;
        
        	public function __construct() {
        		$this->connection = new mysqli($this->db_server, $this->db_username, $this->db_password);
        		if ($this->connection->connect_error)
        			die('Connect Error (' . $this->connection->connect_errno . ') '. $this->connection->connect_error);
        		if(!$this->connection->select_db($this->db_name))
        			$this->createDB();
        		$this->connection->select_db($this->db_name);
        		if(!$this->connection->query('SELECT * FROM member'))
        			$this->createMember();
        	}
        
        	private function createDB() {
        		$create_db = "CREATE DATABASE $this->db_name";
        		echo 'database being created. <br />';
        		return $this->connection->query($create_db);
        	}
        
        	private function createMember() {
        		$create_table_member = "CREATE TABLE member (
        						id int NOT NULL auto_increment PRIMARY KEY,
        						user_name varchar(20) NOT NULL default '' UNIQUE,
        						password char(32) binary NOT NULL default '',
        						cookie char(32) binary NOT NULL default '',
        						session char(32) binary NOT NULL default '',
        						ip varchar(15) binary NOT NULL default '',
        						administrator tinyint(1) NOT NULL
        						)";
        		echo 'table being created <br />';
        		return $this->connection->query($create_table_member);
        	}
        }
        
        $link = new DBConfig();
        ?>

        Duncan

          Write a Reply...