Home of all things tech!
Using OO-PHP To Connect To A Database
With OO-PHP, you can dramatically speed up the coding process, and re-use code much more effectively. OO-PHP stands for Object Oriented PHP. This means it uses objects, which are collections of properties and methods, rather that a bunch of variables and functions.
Also, as the object is stored in an external file, it can be accessed from any of your pages and used anywhere on your site. This makes objects especially useful. A good example of this is using an object to connect to and interact with a database. In this example, I will use MySQL; but the beauty of objects is that you can change that one file and all the pages will be updated, which makes changing between databases easy!
Now, if you were using procedural code, you would connect to MySQL and perform a query like this:
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "pass";
$dbname = "dbname";
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not CONNECT: " . mysql_error());
$dbselect = mysql_select_db($dbname, $con) or die("Could not SELECT DB: " . mysql_error());
$sql = "SELECT * FROM tbl_name WHERE col_name = 'foo'";
$query = mysql_query($sql) or die("Could not EXECUTE QUERY: " . mysql_error());
while($row = mysql_fetch_array($query))
{
echo $row['row_name'];
}
mysql_close($con);
That seems VERY long, doesn’t it? If you use OOP, you could perform exactly the same connection, query, fetch and close just by doing this:
$db = new MySQL;
$result = $db->query("SELECT * FROM tbl_name WHERE col_name = 'foo'");
while($row = $result->fetch())
{
echo $row['row_name'];
}
$db->close();
That’s a lot nicer, isn’t it? But how can it be that easy? Well, the secret is in the MySQL class. You see this line here:
$db = new MySQL;
That’s the line that creates an instance of the MySQL class. What is the MySQL class? Well, it looks like this:
class MySQL
{
var $dbName;
var $dbConn;
var $dbSelect;
var $dbQuery;
public function MySQL($dbName="default_db_name")
{
$this->dbName = $dbName;
$this->connectToDb();
}
private function connectToDb()
{
$this->dbConn = mysql_connect("localhost", "user", "pass");
if(!$this->dbConn)
{
die("Could not CONNECT: " . mysql_error());
}
$this->dbSelect = mysql_select_db($this->dbName, $this->dbConn);
if(!$this->dbSelect)
{
die("Could not SELECT DB: " . mysql_error());
}
}
public function query($sql)
{
$this->theQuery = mysql_query($sql);
if(!$this->theQuery)
{
die("Could not EXECUTE QUERY: " . $sql . " - " . mysql_error());
}
return new MySQLResult($this, $this->theQuery);
}
public function close()
{
mysql_close($this->dbConn);
}
}
The class is the bit that does all the magic, it connects to the server, selects the database, and executes the queries. It also does all the error-catching and “tidying up”.
When it is finished with the query, it passes it to the MySQLResult class, which handles the processing of the results. The MySQLResult class looks like this:
class MySQLResult
{
var $MySQL;
var $theQuery;
public function MySQLResult($MySQL, $theQuery)
{
$this->MySQL = $MySQL;
$this->theQuery = $theQuery;
}
public function fetch()
{
if ($row = mysql_fetch_array($this->theQuery))
{
return $row;
}
}
Together, these two classes make it much easier to connect to and interact with the database, and can make your code much shorter. This causes less bugs and there are less spelling mistakes and syntax errors. This is why I believe OO-PHP is the way to go, and these classes are ready to go as they are, they only need changing so the username and password work for your database, and you’re off! Feel free to use them and speed up your coding!
| Print article | This entry was posted by MattyBatty on July 20, 2009 at 6:10 pm, and is filed under Tutorials. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |