|
|
|
|
|
|
| |
This class is for PHP 5+ only, and uses mysqli functions
| <?php
/**
* Contains the MySQL class
* @package classes
*
*/
/**
* A wrapper for all functions necessary to access a MySQL database and manipulate the results.
*
* @package classes
* @author Nicolas Connault
*/
class MySQL
{
var $host ;
var $database;
var $user ;
var $pass ;
/**
* @var string link_id Points to the selected Database Connection.
*/
var $link_id;
/**
* @var string Result Points to the last Query's Result (not an array)
*/
var $Result;
/**
* @var array Record Contains 1 row from the last Query's Result
*/
var $Record = array();
/**
* @var array data_array Contains ALL rows from the last Query's Result
*/
var $data_array = array();
/**
* @var int row Keeps track of the current row being pointed to by $this->Result
*/
var $row;
/**
* @var string query The last Query called
*/
var $query;
/**
* Constructor function
* Connects to our Database using our username and password (not very secure) and selects the appropriate Database
*
* @return Boolean True
*/
function __construct()
{
$this->host = '127.0.0.1';
$this->user = 'root';
$this->pass = '';
$this->database = 'db';
$this->link_id = mysqli_connect($host,$user ,$pass) or $this->Error(mysqli_error());
$DB = mysqli_select_db($this->link_id,'sweetpeadesigns_com_au_-_pos') or $this->Error(mysqli_error());
return true;
}
/**
* Performs an SQL query $sql and saves the result identifier as $this->Result
* If the SQL query isn't valid, the function Error() is called, which displays the error message and code
* @param string $sql SQL query
* @param string $location Location from which this query is being called.
* @return string Result identifier
*/
function Query($sql, $file, $line, $location)
{
$this->Result = mysqli_query($this->link_id,$sql) or $this->Error(mysqli_error($this->link_id), $location, $file, $line, $sql);
$this->query = $sql;
# DEBUG print $sql;
return $this->Result;
}
/**
* Fetches the current $row from the current $Result as an associative array $this->Record.
* This is an associative array, it has no number indices. If you want number indices, use FetchRow() instead.
* @return array Current Row
*/
function FetchArray()
{
$this->Record = mysqli_fetch_array($this->Result, MYSQLI_ASSOC);
//$this->Clean();
return $this->Record;
}
/**
* Fetches the current $row from the current $Result as a numerical array $this->Record.
* This is a numerical array, it has no associative indices. If you want associative indices, use FetchArray() instead.
* @return array Current Row
*/
function FetchRow()
{
$this->Record = mysqli_fetch_row($this->Result);
return $this->Record;
}
/**
* Moves the Result pointer forward or backward (changes the var $row )
* @param string $type Type of array ("assoc" or "num")
* @param string $order Direction of navigation ("asc" or "desc")
* @return array Current Row
*/
function navigateRow($type = "assoc", $order = "asc")
{
if ($order == "asc")
{
if (mysqli_data_seek($this->Result, $this->row + 1))
{
$this->row += 1;
if ($type == "assoc")
{
$this->Record = mysqli_fetch_assoc($this->Result);
}
elseif ($type == "num")
{
$this->Record = mysqli_fetch_row($this->Result);
}
else
{
print '<p>Error: the function nextRow($type) in the mySQL class takes one argument, either "assoc" or "num".</p>';
}
}
else
{
print '<p>Error: You have reached the last row of the last query (' . $this->query . '). I now reset the result pointer to the first row</p>';
mysqli_data_seek($this->Result, 0);
}
}
elseif ($order == "desc")
{
if (mysqli_data_seek($this->Result, $this->row - 1))
{
$this->row -= 1;
if ($type == "assoc")
{
$this->Record = mysqli_fetch_assoc($this->Result);
}
elseif ($type == "num")
{
$this->Record = mysqli_fetch_row($this->Result);
}
else
{
print '<p>Error: the function nextRow($type) in the mySQL class takes two arguments, type as either "assoc" or "num", and order as either "asc" or "desc".</p>';
}
}
else
{
print '<p>Error: You have reached the first row of the last query (' . $this->query . '). I now reset the result pointer to the last row</p>';
mysqli_data_seek($this->Result, mysqli_num_rows($this->Result) - 1);
}
}
return $this->Record;
}
/**
* "Cleans" the current row ($Record array) by stripping slashes from it
* You would use this when a string might have been saved with slashes to escape special characters
* @return array Current Row
function Clean()
{
foreach(@$this->Record as $Key => $Val)
{
$this->Record[$Key] = stripslashes($Val);
}
return $this->Record;
}
*/
/**
* Fills $data_array with every row of the last Query
* This function is helpful if you want to store the entire result of a SELECT query without having to loop through each row separately.
* @return array Entire result of Query
*/
function fill_Array()
{
for($i = 0; $i < $this->TotalRows(); $i++)
{
mysqli_data_seek($this->Result, $i);
$temp_array[] = mysqli_fetch_row($this->Result) or die("fill_array Query failed: " . mysqli_error($this->link_id));
}
if(isset($temp_array[0]))
{
foreach ($temp_array as $k => $v)
{
foreach ($v as $k2 => $v2)
{
$this->data_array[] = $v2;
}
}
return $this->data_array;
}
}
function TotalRows()
{
$this->Rows = mysqli_num_rows($this->Result);
return $this->Rows;
}
/**
* Print the provided $record in a formatted HTML table.
* The $record can be any array, but mostly a result of an SQL query.
* E.g: $this->Record or $this->data_array
* @param array $record Any array
*/
function Print_Result($record)
{
print "<center><table border=\"1\" bgcolor=\"#F9FABC\" name=\"results\">\n<tr>\n";
foreach($record as $k => $v)
{
print "\t<th bgcolor=\"#A9D5E2\">" . $k . "</th>\n"; //Prints each Column's title
}
print "\t</tr>\n<tr>";
foreach($record as $k => $v)
{
print "\t<td>" . $v . "</td>\n";
}
print "</tr>\n</table></center>\n\n";
}
/**
* Closes the MySQL connection
* The $record can be any array, but mostly a result of an SQL query. E.g: $this->Record or $this->data_array
* @return boolean True if successful
*/
function Close()
{
mysqli_close($this->link_id);
return true;
}
/**
* Displays the provided $Text as a MySQL Query error message
* The text will be a generic mysqli_error() message with the location from which the query was called.
* @param string $Text MySQL Error Message
* @param string $location The location from which the query was called
*/
function Error($Text, $location = "not given", $file = "unknown file", $line = "unknown line", $sql)
{
echo '<div align="left">';
echo "<em>MySQL Error!</em>
<ul>
<li><em>File</em>: $file</li>
<li><em>Line</em>: $line</li>
<li><em>Section</em>: $location</li>
</ul>";
echo '<hr width="40%">';
echo $Text;
echo "<p>$sql</p>";
echo '</div>';
exit();
}
/**
* Unsets all unnecessary variables related to the MySQL functions
*/
function removeMysqlVars()
{
unset ($this->link_id);
unset ($this->Result);
unset ($this->Record);
unset ($this->data_array);
unset ($this->row);
unset ($this->query);
}
function getInfo($result)
{
return mysqli_info($result) ;
}
}
?> | |
Usage Example
| <?php
$myDB = new MySQL();
$query = "SELECT * FROM users WHERE ID > 12";
// The fourth argument in the query function is for when you call this
// function within another function or method (eg. search::get_user)
$myDB->Query($query,__FILE__,__LINE__,'');
for($i = 0; $i < $myDB->TotalRows(); $i ++)
{
$array = $myDB->FetchArray();
$record[$i]['field1'] = $array['field1'];
$record[$i]['field2'] = $array['field2'];
}
?> | | |
|
| MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | | | Online Automatic Class Generator for MySQL Tables Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | | | Authorize Me! An authentication script. Categories : MySQL, Databases, Authentication, PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Tropicalm Genetree Family (MySQL based family tree) Categories : PHP, Interfaces, Databases, MySQL, Complete Programs | | | mysql_escape_string Categories : PHP, MySQL, Databases, Strings | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | |
|
|
|