Easy way to use PHP + MySQL


This script is petty advanced if you do not know much bout PHP and MySQL, but if you follow my directions, you should be able to get it to work with no problems.

I found this code on another website and modifid it to work better.

1) Make a folder named "includes" in the root of your site.
2) Make a file called "SystemComponent.php" and put this code in it replacing HOST, USERNAME, PASSWORD, and DATABASE with the correct values for your sever.
PHP Code
  1. <?php
  2. class SystemComponent {
  3.  
  4. var $settings;
  5.  
  6. function getSettings() {
  7.  
  8. $settings['dbhost'] = 'HOST';
  9. $settings['dbusername'] = 'USERNAME';
  10. $settings['dbpassword'] = 'PASSWORD';
  11. $settings['dbname'] = 'DATABASE';
  12.  
  13. return $settings;
  14.  
  15. }
  16.  
  17. }
  18. ?>

3) Make another file in the includs folder called "DbConector.php" an put the following script in it.
PHP Code
  1. <?php
  2. require_once 'SystemComponent.php';
  3.  
  4. class DbConnector extends SystemComponent {
  5.  
  6. var $theQuery;
  7. var $link;
  8.  
  9. function DbConnector(){
  10. $settings = SystemComponent::getSettings();
  11.  
  12. $host = $settings['dbhost'];
  13. $db = $settings['dbname'];
  14. $user = $settings['dbusername'];
  15. $pass = $settings['dbpassword'];
  16.  
  17. $this->link = mysql_connect($host, $user, $pass);
  18. mysql_select_db($db);
  19. register_shutdown_function(array(&$this, 'close'));
  20. }
  21. function query($query) {
  22. $this->theQuery = $query;
  23. return mysql_query($query, $this->link);
  24. }
  25. function fetchArray($result) {
  26. return mysql_fetch_array($result);
  27. }
  28. function close() {
  29. mysql_close($this->link);
  30. }
  31. }
  32. ?>

4) Now in every page you want to connect to your database, just add the following lines.
PHP Code
  1. require_once("includes/DbConnector.php");
  2. $conn = new DbConnector();

5) This script wll automatically connect to the database when you put those two lines of code into a page. Then you can use "$query = $conn->query('QUERY GOES HERE');" to run a query and "$query = $conn->fetchArray($query);" to fetch an array from the query just like with "mysql_query" and "mysql_fetch_array." You can also put "$conn->close();" at the end of the page to close the MySQL connection.

I hope this makes your MySQL integration easier!
Xerofait's Avatar
Author:
Views:
2,434
Rating:
Posted on Monday 24th March 2008 at 04:29 PM
Enros
Enros's Avatar
Ahhh why did you do that to connect to your db ?


Why didnt you just go :

<?PHP
//Variables
$db_user = "db_user";//User name for the database
$db_pass = "password";//Password for the database
$db_host = "localhost";//Database host, mostly localhost
$db_name = "db_name";//the database name

//Connection with the Varibles
$mysql_access = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);

?>


Then the file in which you put this in could be called connect.php


Then at the top of each of your pages all youd need to put is include('connect.php');


I think that would be wayyyy simpler...