Page 1 of 2

MySQL wrapper class problem

Posted: Thu Nov 27, 2014 8:11 am
by PHPHelpNeeded
Hi,

I am posting the MySQL wrapper class I started:

Code: Select all


        //wrapper class
	class MySQL_Wrapper {
		private $db = null;
		private $dbname = null;
		public function __construct($dbname, $dbhost, $dbuser, $dbpass){
			$this->db = mysql_pconnect($dbhost, $dbuser, $dbpass);
			if(!$this->db){
				throw new RuntimeException("Error: Could not connect to database. Please try again later.");
			} else $this->dbname = $dbname;
		}
	
		public function db_create(){
			$sql = 'CREATE DATABASE '.$this->dbname;
			if (mysql_query($sql, $this->db)) {
				echo "Database {$this->dbname} created successfully\n";
				return true;
			} else {
				echo 'Error creating database: ' . mysql_error() . "\n";
				return false;
			}
		}
	
		public function db_delete(){
			$sql = 'DROP DATABASE '.$this->dbname;
			if (mysql_query($sql, $this->db)) {
				echo "Database {$this->dbname} deleted successfully\n";
				return true;
			} else {
				echo 'Error creating database: ' . mysql_error() . "\n";
				return false;
			}
		}
			
		public function db_close(){
			return mysql_close($this->db);
		}
	
	}

      //php code which is giving me problem, or is it the class how it is defined
       				try{

     					$sql = new MySQL_Wrapper("UDatabase", "localhost", "root", "123456789mysql");

        				if($sql->db_create()){
        					Echo "Now you want to enter information\n";
         				}
	
       	        			if($sql->db_delete()){
       		         			Echo "database was deleted!";
         				}
	
        				$sql->db_close();
       				} catch(RuntimeException $e){
       					die($e->getMessage());
   				}


But I get these error messages:

Code: Select all

[27-Nov-2014 09:02:10 America/New_York] PHP Warning:  Missing argument 1 for MySQL_Wrapper::__construct(), called in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 133 and defined in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 6
[27-Nov-2014 09:02:10 America/New_York] PHP Warning:  Missing argument 2 for MySQL_Wrapper::__construct(), called in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 133 and defined in F:\Eclipse\Eclipse_Projects\PHP_ChessEditor\Server\Console\server.php on line 6
[27-Nov-2014 09:02:10 America/New_York] PHP Warning:  Missing argument 3 for MySQL_Wrapper::__construct(), called in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 133 and defined in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 6
[27-Nov-2014 09:02:10 America/New_York] PHP Warning:  Missing argument 4 for MySQL_Wrapper::__construct(), called in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 133 and defined in F:\Eclipse\Eclipse_Projects\PHP_ChessEditor\Server\Console\server.php on line 6
[27-Nov-2014 09:02:10 America/New_York] PHP Notice:  Undefined variable: dbhost in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 7
[27-Nov-2014 09:02:10 America/New_York] PHP Notice:  Undefined variable: dbuser in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 7
[27-Nov-2014 09:02:10 America/New_York] PHP Notice:  Undefined variable: dbpass in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 7
[27-Nov-2014 09:02:10 America/New_York] PHP Warning:  mysql_pconnect(): Access denied for user ''@'localhost' (using password: NO) in F:\Eclipse\Eclipse_Projects\PHP_ChessEditor\Server\Console\server.php on line 7
[27-Nov-2014 09:02:10 America/New_York] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Error: Could not connect to database. Please try again later.' in F:\Eclipse\PHP_Project1\Server\Console\server.php:9
Stack trace:
#0 F:\Eclipse\PHP_Project1\Server\Console\server.php(133): MySQL_Wrapper->__construct()
#1 {main}
  thrown in F:\Eclipse\PHP_Project1\Server\Console\server.php on line 9

Can anyone tell me what's wrong?

UPDATE: I was looking at my MySQL phpinfo() settings, and it missing the MySQL.default_host, user, password, etc. should I edit that into the php.ini file or is that supposed to stay that way?

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 8:24 am
by Celauran
Code above looks fine. Tested locally and it works fine, too. Given the line numbers referenced in the error messages, the posted code is not the complete code. Does the code as posted work on your machine? Trying to ascertain if the problem lies with code not posted here or if it's some kind of configuration issue.

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 8:25 am
by Celauran
As an aside, the MySQL driver has been deprecated and replaced by MySQLi. PDO is probably an even better choice to build on top of.

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 8:40 am
by PHPHelpNeeded
oh man but I installed MySQL not MySQLi.

I will have to do the install all over again with MySQLi?

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 8:48 am
by Celauran
MySQL itself is fine. It's just the PHP driver. You almost certainly have MySQLi and PDO available, if not active. Mostly just a question of uncommenting some .dll entries in your php.ini

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 8:49 am
by PHPHelpNeeded
Ah never mind, I fixed it.

You cannot have the MySQL_connect() function call in the __construct() function.

But I have a question, how do you create the db without it going into prompt asking you "Do you want to enter information"

How do you avoid that, since I am running a socket server, the server will stall and not receive information because MySQL is prompting.

Help! please....

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 8:51 am
by PHPHelpNeeded
Celauran wrote:MySQL itself is fine. It's just the PHP driver. You almost certainly have MySQLi and PDO available, if not active. Mostly just a question of uncommenting some .dll entries in your php.ini
I checked my php.ini file they are all enabled.

Now to run MySQLi do you just install MySQL like I had previously, or is there a different installation for MySQLi to be able to run it?

Or do I just start using the MySQLi function and they would work?

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 9:26 am
by Celauran
PHPHelpNeeded wrote:Or do I just start using the MySQLi function and they would work?
This. Before you get too deep into it, I will again recommend PDO over MySQLi.

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 10:34 am
by PHPHelpNeeded
Ok, how do I install PDO?

I did find out that the MySQL install I had works for MySQLi.

But if you are saying I should do PDO, can you give me a link with the installation process that I can follow from here?

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 10:44 am
by Celauran
http://php.net/manual/en/book.pdo.php

There shouldn't be anything to install.

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 11:11 am
by PHPHelpNeeded
OK, well let me ask you this, how do you create a root user, or any user at all to create databases?

And with PDO() construct, how do you specify which database to you whether MySQL, MySQLi, or PG, or SQLite? Cause the examples I found at php.net manual, only use MySQL specific dsn string?

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 11:32 am
by PHPHelpNeeded
Ok, but which one dsn string should I use to define the type of database driver, #1, #2, #3 example below?:

Code: Select all

             $db = new PDO("mysql:dbname=test;host=127.0.0.1;", "USER", "PASSWORD"); //#1
             $db = new PDO("mysqli:dbname=test;host=127.0.0.1;", "USER", "PASSWORD"); //#2
             $db = new PDO("sqlite:dbname=test;host=127.0.0.1;", "USER", "PASSWORD");   //#3

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 11:37 am
by Celauran
PHPHelpNeeded wrote:OK, well let me ask you this, how do you create a root user, or any user at all to create databases?
Your root user should have been created when you installed the database server.

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 11:37 am
by Celauran
PHPHelpNeeded wrote:Ok, but which one dsn string should I use to define the type of database driver, #1, #2, #3 example below?:

Code: Select all

             $db = new PDO("mysql:dbname=test;host=127.0.0.1;", "USER", "PASSWORD"); //#1
             $db = new PDO("mysqli:dbname=test;host=127.0.0.1;", "USER", "PASSWORD"); //#2
             $db = new PDO("sqlite:dbname=test;host=127.0.0.1;", "USER", "PASSWORD");   //#3
#1

Re: MySQL wrapper class problem

Posted: Thu Nov 27, 2014 11:40 am
by PHPHelpNeeded
Celauran wrote:
PHPHelpNeeded wrote:OK, well let me ask you this, how do you create a root user, or any user at all to create databases?
Your root user should have been created when you installed the database server.
Oh ok, I was just wondering if I had to that all over again, good thing I didn't uninstall. Although I could've had reinstall it again.

The server is running fine, so I should be good as long as I use the PDO functions, right?

Thanks I will keep in mind to use number #1.

For anyone interested in wanting to know which link I used to install the database server and which version, follow this link: http://blog.pluralsight.com/install-mysql-on-iis7

Note: I know it says MySQL for IIS, I think it would work for you if you already have my sql with WAMP or ZEND...but since I use IIS, that's what I found thanks to someone who gave me the link earlier.