Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some libraries, such as MDB2 in PEAR that make querying a bit easier and safer.</p> <p>Unfortunately, they can be a bit wordy to set up, and you sometimes have to pass them the same info twice. I've used MDB2 in a couple of projects, and I tended to write a thin veneer around it, especially for specifying the types of fields. I generally make an object that knows about a particular table and its columns, and then a helper function in that fills in field types for me when I call an MDB2 query function.</p> <p>For instance:</p> <pre><code>function MakeTableTypes($TableName, $FieldNames) { $Types = array(); foreach ($FieldNames as $FieldName =&gt; $FieldValue) { $Types[] = $this-&gt;Tables[$TableName]['schema'][$FieldName]['type']; } return $Types; } </code></pre> <p>Obviously this object has a map of table names -> schemas that it knows about, and just extracts the types of the fields you specify, and returns an matching type array suitable for use with an MDB2 query.</p> <p>MDB2 (and similar libraries) then handle the parameter substitution for you, so for update/insert queries, you just build a hash/map from column name to value, and use the 'autoExecute' functions to build and execute the relevant query.</p> <p>For example:</p> <pre><code>function UpdateArticle($Article) { $Types = $this-&gt;MakeTableTypes($table_name, $Article); $res = $this-&gt;MDB2-&gt;extended-&gt;autoExecute($table_name, $Article, MDB2_AUTOQUERY_UPDATE, 'id = '.$this-&gt;MDB2-&gt;quote($Article['id'], 'integer'), $Types); } </code></pre> <p>and MDB2 will build the query, escaping everything properly, etc.</p> <p>I'd recommend measuring performance with MDB2 though, as it pulls in a fair bit of code that might cause you problems if you're not running a PHP accelerator.</p> <p>As I say, the setup overhead seems daunting at first, but once it's done the queries can be simpler/more symbolic to write and (especially) modify. I think MDB2 should know a bit more about your schema, which would simpify some of the commonly used API calls, but you can reduce the annoyance of this by encapsulating the schema yourself, as I mentioned above, and providing simple accessor functions that generate the arrays MDB2 needs to perform these queries.</p> <p>Of course you can just do flat SQL queries as a string using the query() function if you want, so you're not forced to switch over to the full 'MDB2 way' - you can try it out piecemeal, and see if you hate it or not.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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