Categories

LinkedIn

Jonathan Anders

Share

Recent Comments

MySQL Database Handler

How to use

Here is a small snippet of how I used the database class. I have 4 constants that are used, they are:

define("DB_HOSTNAME", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "test_database");

Next, I use the following function to run a query. It returns an array of results.

function dbQuery ($dbQuery) {
  try {
    if (!$dbQuery) { return; }
    $dbResultArray = array();
    $dbConnection = new DBConnection(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_NAME);
 
    if (!$dbConnection->Query($dbQuery)) { die ("Error: problem with query specified."); }
 
    $dbExecQuery = $dbConnection->Query($dbQuery);
 
    while ($dbGetResult = $dbConnection->FetchArray($dbExecQuery)) {
      $dbResultArray[] = $dbGetResult;
    }
 
    if (!is_array($dbResultArray)) {
      return;
    } else {
      return $dbResultArray;
    }
  } catch (Exception $e) {
    die ("Caught exception: " . $e->getMessage());
  }
}

So you could call

dbQuery("SELECT * FROM test")

and it would return an array of results.

Download

Download database.class.php.

 

Popularity: 14% [?]

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Spam protection by WP Captcha-Free