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