Development Wiki

Dbal.sql build array

From phpBB Development Wiki

Revision as of 00:15, 27 August 2012 by Imkingdavid (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

dbal::sql_build_array –– Builds a SQL INSERT or UPDATE Query statement string.


string dbal::sql_build_array ( string $query [, array $assoc_ary ] )

The $db->sql_build_array function is responsible for handling UPDATE or INSERT SQL data safely. This function automatically escapes strings and ensures type safety. When using the sql_build_array, there is no need to escape strings or force variable types. Although forcing variable types is still recommended as a general rule.


Query type, supports:

Parameter Usage
INSERT Building insert statements.
INSERT_SELECT Building query for INSERT INTO table (...) SELECT value, column ... statements.
UPDATE Building update statements.
SELECT Building WHERE statement AND logic.

The data array to create the INSERT or UPDATE SQL Statement string. Array keys are used for field names.

Return Values

Returns a string containing INSERT or UPDATE syntax to be used within an SQL Query.


Example #1 Inserting a new row into a database table

$sql_ary = array(
'somedata'      => $my_string,
'otherdata'     => $an_int,
'moredata'      => $another_int

$sql 'INSERT INTO ' SOME_TABLE ' ' $db->sql_build_array('INSERT'$sql_ary);


Example #2 Updating a database table row

$sql_ary = array(
'somedata'      => $my_string,
'otherdata'     => $an_int,
'moredata'      => $another_int

    SET ' 
$db->sql_build_array('UPDATE'$sql_ary) . '
    WHERE user_id = ' 
. (int) $user_id;

Example #3 Building a SELECT

$sql_ary = array(
'somedata'      => $my_string,
'otherdata'     => $an_int,
'moredata'      => $another_int

$sql 'SELECT some_field
        FROM ' 
        WHERE ' 
$result $db->sql_query($sql);

while (
$row $db->sql_fetchrow($result))
$template->assign_block_vars('some_block', array(
'SOMEDATA'    => $row['somedata'],

See Also