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.
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
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
Flattens all nested arrays into a single array
Returns a string of question marks separated by commas whose list length is equal to the array length of the parameter
$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: '?, ?, ?'
Returns a string in the format “NAME=?, NAME=?, ..., NAME=?”
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
Returns a string in the format “(`NAME1`, `NAME2`, ..., `NAME#`), (?, ?, ..., ?)”
PrepareInsertList(array_keys($Values))//See $Values from above example
Will return: '(`a`, `b`, `c`) VALUES (?, ?, ?)'
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
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 static member $InitialPrintAndDieOnError which DSQL.PrintAndDieOnError inherits on creation
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.