It's some very old work, but still a good base, seeing as mysql_ functions will soon be completely depreciated from PHP.
Honestly, not even 100% if this works, I can help with any problems, other than using this over mysql, I recommend PDO.
Code:
<?php
/**
* Database class
*/
class Database
{
/**
* @const The database host
*/
const DATABASE_HOST = 'localhost';
/**
* @const The database username
*/
const DATABASE_USER = 'root';
/**
* @const The database password
*/
const DATABASE_PASS = '';
/**
* @const The database port
*/
const DATABASE_PORT = 3306;
/**
* @const The database name
*/
const DATABASE_NAME = 'mysqli';
/**
* @private $connection The database connection
*/
private $connection;
/**
* @private $query The last preformed MySQL Query
*/
private $query;
/**
* @var $error The last error the query
*/
var $error;
/**
* Constructs the MySQL connection, connecting to the database
*/
function __construct()
{
$this->connection = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, NULL, DATABASE_PORT);
if ($this->connection->connect_error)
{
$this->error = "Unable to connect to MySQL Server.\n<!--\nMySQLi Said:\n" . $this->connection->connect_error . "\n-->\n";
return false;
}
if (!$this->select_db())
{
$this->error = "Unable to select database.";
return false;
}
return true;
}
/**
* Selects the database we're going to use
*
* @return boolean
*/
private function select_db()
{
return $this->connection->select_db(DATABASE_NAME);
}
/**
* Executes a simple MySQL Query
*
* @param string $query The MySQL Query to execute
* @return mysql
*/
function query($query)
{
$this->query = $query;
$debug_text = "";
$result = null;
if ($result = $this->connection->query($query))
{
$MYSQL_QUERIES += 1;
return $result;
}
else
{
$this->error = $this->connection->error;
if (DEBUG)
{
$debug_text .= "\n<!--\nMySQL Query Error:\n";
$debug_text .= "Error number: " . $this->connection->errno . "\n";
$debug_text .= "Error: " . $this->connection->error . "\n";
$debug_text .= "Query: $this->query\n-->\n";
echo $debug_text;
}
return false;
}
}
/**
* Fetches data from a MySQL result
*
* @param mysql $result The MySQL query resultset
* @param int $row The row number to retrieve
* @param mixed $field The field to retrieve
* @return mixed
*/
function result($result, $row, $field = 0)
{
$result->data_seek($row);
$assoc = $this->fetch_assoc();
return $assoc[$field];
}
/**
* Returns associative array corresponding to the row
*
* @param mysql $result The MySQL query resultset
* @return array
*/
function fetch_assoc($result)
{
return $result->fetch_assoc();
}
/**
* Returns array of strings corresponding to the row
*
* @param mysql $result The MySQL query resultset
* @return array
*/
function fetch_array($result)
{
$results = array();
while ($row = $result->fetch_assoc())
{
$results[] = $row;
}
return $results;
}
/**
* Returns the count of rows from a result
*
* @param mysql $result The MySQL query resultset
* @return int
*/
function num_rows($result)
{
return $result->num_rows;
}
/**
* Returns the count of affected rows from a result
*
* @return int
*/
function affected_rows()
{
return $this->connection->affected_rows;
}
/**
* Returns the last insert ID
*
* @return int
*/
function last_insertid()
{
return $this->connection->insert_id;
}
/**
* Gets the MySQLi connection
*
* @return $mysqli
*/
function get_connection()
{
return $connection;
}
/**
* Escapes a string for MySQL
*
* @param mixed $string The string to be escaped
* @return mixed
*/
function escape($string)
{
if (is_array($string))
{
foreach ($string as $key => $value)
{
$string[$key] = get_magic_quotes_gpc() ? $this->connection->escape_string(stripslashes($value)) : $this->connection->escape_string($value);
}
return $string;
}
else
{
return get_magic_quotes_gpc() ? $this->connection->escape_string(stripslashes($string)) : $this->connection->escape_string($string);
}
}
}
$db = new Database;
MySQLi documentation
You need the MySQLi library, which you should have, if not I recommend upgrading your PHP version, if it's disabled, just enable it in php.ini
To use the class, use it just like MySQL really.
Code:
$db->query("SELECT * FROM `users` WHERE `username` = '" . escape($_POST['username']) ."');
You can do prepared statements with MySQLi too, which is really better.
When binding parameters, you use a question mark indicating there's going to be data inserted, then when you use the binding function, the type is indicated by a string;
Code:
Character Description
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
MySQLi - Prepared statements
An example of a prepared statement:
Code:
$mysqli = $db->get_connection();
$stmt = $mysqli->prepare("SELECT * FROM `users` WHERE `username` = ? AND `userid` = ?"); // prepares to select where username = ?
$stmt->bind_param("s", $_POST['username']); // binds "s" (string) to the first param
$stmt->bind_param("i", $_POST['userid']); // binds "i" (integer) to the second param
$stmt->execute(); // executes the statement
while ($row = $stmt->fetch()) {
echo "User email: $row[email]";
}
$stmt->close(); // closes the statement
Idk, somebody mentioned it in the shoutbox.