Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you do plan on making a database class it may be an idea looking into making it a singleton, allowing you to use it without declaring it/creating it, as...</p> <pre><code>global $db; $db = new db; $db-&gt;query ('... sql ...'); </code></pre> <p>is kinda redundant when you can do</p> <pre><code>db::query ('... sql ...'); </code></pre> <p>I have a set of SQL functions that I use on a near regular basis to reduce what used to be a multi-line escaped lot of SQL to a single call, for example:</p> <pre><code>get_element ($table, $element, $value, $column='id'); get_row ($table, $value, $column='id'); </code></pre> <p>So if you just want to get the name from a table 'customers' where the id is 4 you:</p> <pre><code>$name = db::get_element ('customers', 'name', 4); </code></pre> <p>There are also accompanying functions query_element and query_row, where you just pass it an SQL string and it returns a single element/row.</p> <p>Along with the functions for insert/update, e.g.</p> <pre><code>$array = array ( 'name' =&gt; 'bob jones', 'age' =&gt; 28 ); $insert_id = db::insert_array ('customers', $array); $customer_details = db::get_row ('customers', $insert_id); $customer_details['age'] = 30; db:update_array ('customers, $customer_details); </code></pre> <p>Would create a new row, pull the details back out, update the age, then re-write it to the database.</p> <p>Creating custom SQL access modules on a per-table basis is generally a mistake I have always found - it's better to just generically query the database using sensible functions.</p> <p>If you do have to use anything with complex joins then it is always best to create function for it getCustomerInfo () for example, but if you just want a generic table value lookup making lots of custom methods just increases the chances of mistakes in one of them. Plus escaping data is very important - if you can use non-sql as much as possible and funnel it through a few core functions you can ensure everything is properly escaped fairly easily.</p> <p>If you want to look at my custom database class let me know.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload