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:
1 2 3 4 5 6 7 8 9 10 11 12
|
<?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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
<?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
|
Ray Soucy works as a Communications Specialist for the University of Maine System.
The views expressed herein are those of the author and do not necessarily reflect those of the University of Maine System.
This site is published by Ray Soucy, 239 Forest Avenue, Orono, ME, 04473.
Copyright © 2009, by Ray Patrick Soucy.
Previous editions © 2004, 2005, 2006, 2007, and 2008.
Verbatim copying and distribution of this site is permitted for non-commercial use provided this notice is preserved.
All other rights reserved.
This page was accessed on 2010-09-06 00:44:19 EDT by 38.107.191.92