Web Development
Discuss the technologies surrounding the world wide web.

Go Back   The World of Game Hacking > Community > Web Development

IRC Rules
Post New Thread  Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
n00bie
 
Tuesday's Avatar
 


16-Bit Member

 
Posts: 19
Join Date: Jul 2008
Last Online: 12-27-2009 01:04 PM
Reputation: Tuesday is on a distinguished road
User is Offline
   
PHP - Database connection class - 12-27-2008, 05:20 PM

Hi there. Today I'm going to learn you about Mysql Database connection using a class.

Requirements
- PHP5 Server (not 4 )
- Basic knowledge of PHP (See my other tutorial)



Normally you would connect to a mysql database like this:
PHP Code:

			
$link mysql_connect("localhost""user""password");

if (
$link) {
        die(
'Could not connect: ' mysql_error());
}
mysql_select_db("Databasename");
$result mysql_query("SELECT * FROM `tablename` LIMIT 30");
mysql_close($link); 
However, that's a little bit messy and it would be useless if you have to place mysql_connect/close in every PHP file. And if your user or password changed, you have to edit ALL the PHP files... isn't nice.

So you can do this with a class and a config file (config.php). For example:

config.php
PHP Code:

			
<?php

    define
("DB_HOST""localhost");
    
define("DB_USER""username");
    
define("DB_PW""password");
    
define("DB_NAME""databasename");
?>
database.php
PHP Code:

			
<?php

class database {
    private 
$dblink;
    function 
__construct($configfile) {
        require_once(
$configfile);
        
$this->dblink mysql_connect(DB_HOSTDB_USERDB_PW);
        if (!
$this->dblink) {
            exit(
'Could not connect: ' mysql_error());
        }
        
mysql_select_db(DB_NAME);
    }
    function 
query($sql) {
        
$res mysql_query($sql);
        if (!
$res) {
            echo 
"<strong>Error:</strong> ".mysql_error()."\n<br />";
        }
        return 
$res;
    }
    function 
count_rows($sql) {
        
$res mysql_query($sql);
        
$crows 0;
        if (
$res$crows mysql_num_rows($res);
        return 
$crows;
    }
    function 
__destruct() {
        
mysql_close($this->dblink);
    }
}
?>
index.php
PHP Code:

			
require_once('database.php');

$db = new database('config.php');
echo 
$db->count_rows("SELECT * FROM tablename LIMIT 100");
$result $db->query('SELECT * FROM tablename LIMIT 100'); 
As you can see it's shorter and it has 1 config file.
The database class could use a better error handling, but it's just an example, don't shoot me.
Good luck,
Tuesday


Advancement in position
And that was when I ruled the world

Last edited by Tuesday; 11-05-2009 at 08:50 AM.. Reason: PHP highlighting added
  
Reply With Quote
  (#2 (permalink)) Old
DivineBanhammer
 
RiSiCO's Avatar
 


256-Bit Member

 
Posts: 1,286
Join Date: Jul 1984
Location: [root@localhost~]
Last Online: 03-09-2010 01:58 AM
Reputation: RiSiCO is a jewel in the roughRiSiCO is a jewel in the roughRiSiCO is a jewel in the roughRiSiCO is a jewel in the rough
User is Offline
romania
  Send a message via AIM to RiSiCO Send a message via MSN to RiSiCO Send a message via Yahoo to RiSiCO Send a message via Skype™ to RiSiCO 
12-27-2008, 06:21 PM

A good tutorial, as always


»Please Read our Rules and WOGH Etiquette & Guidelines «
» Help me continue WOGH development, donate«
» Do not PM me for requests! «



  
Reply With Quote
  (#3 (permalink)) Old
n00bie
 


8-Bit Member

 
Posts: 1
Join Date: Oct 2009
Last Online: 11-05-2009 01:57 PM
Reputation: dikamilo is on a distinguished road
User is Offline
   
10-31-2009, 03:30 PM

Better use PDO in php5, it's easier and works with a lot of db.

PHP: PDO - Manual

Example
Code:
$pdo = new PDO('mysql:host=localhost;dbname=myname', 'user', 'password');

$pdo -> query('SELECT * FROM users');
  
Reply With Quote
  (#4 (permalink)) Old
n00bie
 
Tuesday's Avatar
 


16-Bit Member

 
Posts: 19
Join Date: Jul 2008
Last Online: 12-27-2009 01:04 PM
Reputation: Tuesday is on a distinguished road
User is Offline
   
11-05-2009, 08:58 AM

Quote Originally Posted by dikamilo View Post
Better use PDO in php5, it's easier and works with a lot of db.

PHP: PDO - Manual
Yes that's also a way to do it.

I added code highlighting (so using [ php ] and [ html ] instead of [ code ]) to all my tutorials:
Basics of PHP
Basic Javascript animations
PHP5 - Basics of Classes


Advancement in position
And that was when I ruled the world
  
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes




New To Site? Need Help?


All times are GMT +1. The time now is 03:31 AM.


Powered by vBulletin
Copyright ©1995 - 2009 GameHacking.com & CES