dev-master
9999999-dev https://github.com/epifrin/mysql.classPHP class to work with a MySQL database
MIT
The Requires
- php >=5.2.0
Wallogit.com
2017 © Pedro Peláez
PHP class to work with a MySQL database
Connect to database and create object, (*1)
require_once '/path/to/mysql.class.php';
$Query = MySqlClass::connect('localhost','dbname','dbuser','dbpass','utf8');
Simple sql query, (*2)
$Query->sql('INSERT INTO tablename VALUES ("value1","value2")');
->query() alias of sql()
Get an array of the first row of the table, (*3)
$array = $Query->getarray('SELECT * FROM tablename');
->ga() alias of getarray()
Get an multiple array of records of the table, (*4)
$array = $Query->getmultiarray('SELECT * FROM tablename');
->gma() is alias of getmultiarray()
Get a value of first field of first record, (*5)
$value = $Query->getvalue('SELECT * FROM tablename');
->gv() is alias of getvalue()
Get an array of values of first column, (*6)
$array = $Query->getverticalarray('SELECT * FROM tablename');
->gva() is alias of getverticalarray()
Get index array with keys from values first field, (*7)
$array = $Query->getindexmultiarray('SELECT * FROM tablename');
->gima() is alias of getindexmultiarray()
Get text of last SQL query, (*8)
$sqltext = $Query->getLastQuery();
Additional functions, (*9)
//incoming text is processed using functions: stripslashes, mysql_real_escape_string
$Query->check_sql($string)
//incoming text is processed using functions: stripslashes, mysql_real_escape_string, htmlspecialchars
$Query->check_text($string)
Example
$Query->sql('INSERT INTO tablename VALUES ("'.$Query->check_text($_POST['param1']).'", "'.$Query->check_text($_POST['param2']).'")');
$Query->check_date($date) // example right date 2013-05-15
$Query->check_time($time) // example right time 13:05:45
$Query->check_datetime($datetime) // example right datetime 2013-05-15 13:05:45
Error message, (*10)
If you set email for object property mailForError, you will recieve error message, (*11)
$Query->mailForError = 'my_mail@domain.com';
Also, you can set alternative error handler function, (*12)
Example, (*13)
$Query->functionErrorName = 'mysqlErrorFunctionName'; // set name of error handler function
/**
* error handler
*
* @param string $msg Mysql error text
* @param string $sql SQL query
* @param array $arrDebug Debug backtrace
*/
function mysqlErrorFunctionName($msg, $sql, $arrDebug){
$errMsg = '<b>MySQL Error:</b><br>
SQL select: '.$sql.'<br>
Error: '.$msg.'<br>';
$errMsg .= 'Stack trace:<br>';
foreach($arrDebug AS $debug){
$errMsg .= 'File: <b>'.$debug['file'].'</b>, line: <b>'.$debug['line'].'</b><br>';
}
die($errMsg);
}
If you use alternative error handler function, you cannot use property "mailForError", (*14)
PHP class to work with a MySQL database
MIT