Posted by: SEO Positive

Posted on: July 7, 2010 8:23 am

-

While a lot of php programmers still use standard MySQL no problem, I still do, there is MySQLi Which stands for “MySQL improved” and its simply a driver for PHP with more functionality and safer than using your standard MySQL code.

See below for a pretty bog standard PHP class to connect to MySQL

class mysqlCon
{
             private static $connection;

             private function __construct($server, $username, $password, $database)
             {
                          if(!mysql_connect($server,$username,$password))
                                       throw new RunTimeException('Could not connect to MySQL server. MySQL said: '.mysql_error());
                          if(!mysql_select_db($database))
                                       throw new RunTimeException('Could not connect to MySQL database. MySQL said: '.mysql_error());
                          self::$connection = true;
                          return $this;
             }
}
$connection = new mysqlCon('localhost', 'username', 'password', 'database');

Which looks, to most PHP programmers pretty standard. But see below for the MySQLi version of this operation

class mysqliCon
{
             private static $connection;

             private function __construct($server, $username, $password, $database)
             {
                          self::$connection = new mysqli($server, $username, $password, $database);
                          if(self::$connection->error)
                                       throw new RunTimeException('MySQLi said no. It also said: '.self::$connection->error);
                          return $this;
             }
}

Which, as you can see is only half the size, its half the code for twice as much functionality and security.

I recommend a movement to MySQLi because it really is great, there’s not a flaw to it its much easier to use and much easier to learn (And the errors are friendlier)

Authors
Categories
Archives
Blogroll