A MySQL library for PHP with functionality to help facilitate cleaner and quicker SQL access
Rating: 9 (This has been an ongoing library for a long time that has constantly shaped and evolved into exactly what I want and need (Along with DWCF)) Project Time: 2007-2014: Unknown due to alot of changes over time Languages: PHP, MySQL Requirements: PHP, MySQL, PHP MySQL lib Downloads: Source (See any updates below for prior versions) Sections: Information, Content, Concepts, Updates, Comments
I’ve found using php’s included MySQL functions to be cumbersome, take a lot more code than necessary, and create code that is not very readable. I am also not completely happy with PDO, so DSQL is my solution to this problem. I use it heavily in all of my professional web projects.
The classes in this library are written generically so they could easily be converted to any other database software.
$Data=$MyConn->CleanQuery(' SELECT T1.w AS W1, T2.w AS W2, T1.x AS X1, T2.x AS X2 FROM _TBL_data AS T1 INNER JOIN example_data AS T2 ON T2.w=T1.w WHERE T1.x=?',30);
If only 2 return fields are requested with GetKeyed, the value of each item would instead be a scalar
Class DSQL
General information
MySQL interface
Statically called functions use the remembered DSQL object in $GlobalConnection
This allows the [default] DSQL object to not have to be remembered or passed externally. This is useful since generally only 1 database connection is established.
Array('/_TBL_/'=>'MyForum_',//Changes all instance of “_TBL_” to “MyForum_” (useful for adding table prefixes)'/^(INSERT)(\s+)/i'=>'$1 IGNORE$2',//Changes all queries starting with “INSERT ” to “INSERT IGNORE ” (for example: “insert into foo” to “insert INGORE into foo”)'/\s*;?\s*$/'=>''//Removes a semicolon at the end of the query and/or any whitespace at the end)
This happens at the top of the Query() function, so all errors and information regarding the query will reflect these replacements
Object Creation
new DSQL($Server='localhost', $UserName='', $Password='', $Database=NULL)
Connect to the server
If database is not specified, none is selected
In all construct functions, if a parameter is not specified, the default is used
This is used if 2 or more parameters are passed to the constructor
All functions are internally named starting with an underscore to facilitate the dual static/nonstatic calling functionality. For example: DSQL::_Query
Helper (static) functions
FlattenArray($Array)
Flattens all nested arrays into a single array
PrepareList($List)
Returns a string of question marks separated by commas whose list length is equal to the array length of the parameter
For example:
$Values=Array('a'=>5,'b'=>10,'c'=>15);//This is an example array in which the keys are the column names and the values are also the SQL’s field values. This array can be passed directly to Query()PrepareList($Values);//The keys are not needed for this function; only the list length
Will return: '?, ?, ?'
PrepareUpdateList($NameList)
Returns a string in the format “NAME=?, NAME=?, ..., NAME=?”
For example:
PrepareUpdateList(array_keys($Values))//See $Values from above example
Will return: 'a=?, b=?, c=?'
This does not account for reserved field names that need to be enclosed in backticks
PrepareInsertList($NameList)
Returns a string in the format “(`NAME1`, `NAME2`, ..., `NAME#`), (?, ?, ..., ?)”
For example:
PrepareInsertList(array_keys($Values))//See $Values from above example
Will return: '(`a`, `b`, `c`) VALUES (?, ?, ?)'
EscapeSearchField($S)
Create a LIKE search string [for MySQL]
This is done by:
Escaping with a backslash all backslashes, underscores, and percent signs
Adding a percent sign to the beginning and end of the string
For example:
EscapeSearchField('ab%c_d\\e')
Will return:
'%ab\\%c\\_d\\\\e%'
Un-php-stringified: %ab\%c\_d\\e%
Exceptions and Errors
The error functions may be overwritten in a derived class
$Error: The error message
$QueryFormat: The passed query format
$QueryParameters: The passed parameters. This is not flattened on the “Query data count does not match” error
$CompiledQuery: The compiled query with question marks replaced
$StartTime: The unix timestamp of when the query started
)
Added ability via DSQL->$StrictMode variable to update MySQL strict mode
Added DSQL->RawQuery() function which takes just a query, and does no modification on it. I found this required due to extremely slow times on building very large queries.
Added DSQL->EscapeString() function which escapes a string for use in a mysql query. It has an optional parameter that adds single quotes around the result.
Added DSQL::PrepareInsertList() function which takes an array of names, and turns it into the following string: ['a', 'b', 'c'] => “(`a`, `b`, `c`) VALUES (?, ?, ?)”
Added static member $InitialPrintAndDieOnError which DSQL.PrintAndDieOnError inherits on creation
Bug Fixes:
mysqli_set_charset is set to utf-8
In FormatSQLError() the date() function used for “Start Time” now uses “24-hour format of an hour with leading zeros” [date(“H”)] instead of “12-hour format of an hour without leading zeros” [date(“g”)]
I finally decided to get on the bandwagon and go ahead and start submitting my stuff to Github too. DSQL and DWCF are already onthere, and I may or may not start adding a few older ones too. Here is my Dakusan profile.
DSQL is a MySQL library for PHP with functionality to help facilitate cleaner and quicker SQL access. It was originally part of the Other Web Scripts project (called PHP MySQL Library [v1.0 Source]), but since the documentation got so large, and it is one of the two most helpful and utilized libraries I have and use right now, I decided it was time to give it its own page. I use this heavily in all of my professional web projects.
To add comments, please go to the forum page for this project (guest comments are allowed for the Projects, Posts, and Updates Forums). Comments are owned by the user who posted them. We accept no responsibility for the contents of these comments.