MySQL
MaplePHP - MySQL queries is a powerful yet user-friendly library for making safe database queries.
Install the database
Install the database with the MakePHP cli.
php cli config install --type=mysqlMake queries
Once installed you can access the database with the service provider:
$this->db = $this->provider->DB();Then access the DB library:
Select 1:
$select = $this->db::select("id,name,content", "pages")->whereStatusParent(1, 0);
$array = $select->fetch(); // Get all rows as an arraySelect 2:
$select = $this->db::select("id,firstname,lastname", ["users", "aliasA"])->whereId(1)->where("status", 0, ">")->limit(1);
$select->join(["login", "aliasB"], "aliasB.user_id = aliasA.id");
$obj = $select->get(); // Get one row result as objectDefault alias will be the table name e.g. users and login if they were not overwritten.
Where 1
Where 2
"compare", "or"/"and" and "not".
Where 3
Where 4
Having
Having command works the same as where command above with exception that you rename "where" method to "having" and that the method "havingBind" do not exist.
Limit
Order
Join
Note that no value in the join is, by default, enclosed. This means it will not add quotes to strings. This means it will attempt to add a database column by default if it is a string, and will return an error if the string column does not exist. If you want to enclose the value with quotes, use Attributes (see the section below).
Insert
Update on duplicate
Will update row if primary key exist else Insert
Update
Delete
Set
Preview SQL code before executing
Attributes
Each value is automatically escaped by default in the most effective manner to ensure consequential and secure data storage, guarding against SQL injection vulnerabilities. While it's possible to exert complete control over SQL input using various Raw methods, such an approach is not advisable due to the potential for mistakes that could introduce vulnerabilities. A safer alternative is to leverage the Attr class. The Attr class offers comprehensive configuration capabilities for nearly every value in the DB library, as illustrated below:
Escape values and protect against SQL injections
Example:
Input value: Lorem "ipsum" dolor
Output value: Lorem \"ipsum\" dolor
Enable/disable string enclose
Example:
Input value: 1186
Output value: '1186' E.g. will add or remove quotes to values
Enable/disable XSS protection
Some like to have the all the database data already HTML special character escaped.
Example:
Input value: Lorem ipsum dolor
Output value: Lorem <strong>ipsum</strong> dolor
Automatically json encode array data
A pragmatic function that will automatically encode all array input data to a json string
Example:
Input value: array("firstname" => "John", "lastname" => "Doe");
Output value: {"firstname":"John","lastname":"Doe"}
The default values vary based on whether it is a table column, a condition in a WHERE clause, or a value to be set. For instance, if expecting a table columns, the default is not to enclose value with quotes, whereas for WHERE or SET inputs, it defaults is to enclose the values. Regardless, every value defaults to prep, encode and jsonEncode being set to true.
Last updated
Was this helpful?