Ray Patrick Soucy

Easy MySQL for PHP

Release 1.0.3

Sometime you just need a lightweight script to connect to a MySQL database.  For those times, there is this.  A simple class that you can embed your connection data into and use to query your database with one function and get the output as a 2-dimensional associative array.  Available under the GNU General Public License.

Download

API Reference Documentation

Example:

<?php

    require 'EasyMySql.php';
    $db = new EasyMySql('hostname', 'username', 'password', 'database');

    $db->connect();
    $data = query("SELECT field FROM table");
    $db->close();

    print_r($data);

?>

Source:

<?php

/**
 * Easy MySQL for PHP
 *
 * A simple MySQL wrapper class for small projects
 *
 * Copyright (C) 2009 Ray Patrick Soucy
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package   EasyMySql
 * @author    Ray Soucy <rps@soucy.org>
 * @version   1.0.3
 * @copyright 2009 Ray Patrick Soucy
 * @link      http://www.soucy.org/
 * @license   GNU General Public License version 3 or later
 * @since     File available since Release 1.0.3
 */

/**
 * EasyMySql
 * @package    EasyMySql
 * @version    Release: @package_version@
 * @deprecated Class deprecated in Release 2.0.0
 */
class EasyMySql 
{

    private $_hostname;
    private $_username;
    private $_password;
    private $_database;
    private $_connection;
    private $_query;
    private $_result;

    /**
     * Class Constructor
     * @param  string $hostname Hostname or IP address of MySQL server
     * @param  string $username Username for MySQL
     * @param  string $password Password
     * @param  string $database Name of database to use
     * @return object EasyMySql object
     */
    public function __construct($hostname, $username, $password, $database) 
    {
        $this->_hostname = $hostname;
        $this->_username = $username;
        $this->_password = $password;
        $this->_database = $database;
        $this->_query = "";
    } // __construct

    /**
     * Query the database
     * @param  string        $query SQL statement
     * @return mixed|boolean Return an array of associative arrays for result sets, boolean for other functions.
     */
    public function query($query) 
    {
        $this->_query = $query;
        $this->_execute();
        $this->_format();
        return $this->_result;
    } // query

    /**
     * Establish a connection to the MySQL server
     */
    public function connect() 
    {
        $this->_connection = mysql_connect($this->_hostname, $this->_username, $this->_password);
        if ($this->_connection === false) {
            error_log(mysql_error());
            die('Error: Unable to connect to database server.');
        } // if
        if (mysql_select_db($this->_database, $this->_connection) === false) {
            error_log(mysql_error());
            die('Error: Unable to access database.');
        } // if
    } // connect

    /**
     * Execute SQL statement and fetch result
     */
    private function _execute() 
    {
        $this->_result = mysql_query($this->_query, $this->_connection);
    } // _execute

    /**
     * Format result 
     *
     * If resource, translate into a 2-dimensional array (array of associative arrays using field names as 
     * keys).
     */
    private function _format() 
    {
        if (is_resource($this->_result)) {
            $tmp = array();
            while ($row = mysql_fetch_assoc($this->_result)) {
                array_push($tmp, $row);
            } // while
            $this->_result = $tmp;
        } // if
    } // _format 

    /**
     * Close an active MySQL connection
     */
    public function close() 
    {
        mysql_close($this->_connection);
    } // close

} // EasyMySQL

// trailing PHP tag omitted to prevent accidental whitespace