diff options
author | rvelices <rv-github@modusoptimus.com> | 2014-02-11 21:47:44 +0000 |
---|---|---|
committer | rvelices <rv-github@modusoptimus.com> | 2014-02-11 21:47:44 +0000 |
commit | 2d9887993cc2c5dbbc22106c07906220bf33836f (patch) | |
tree | c9b07b57c5ab93d8be9356105e317c051267e8c4 /include/dblayer | |
parent | 78621a34878093fe2fed7bcf76ac61aca9acb249 (diff) |
arrayfromquery optimizations: move double if from inside loop to outside + use directly mysqli calls to avoid function call overhead for every row retrieved from db
git-svn-id: http://piwigo.org/svn/trunk@27336 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'include/dblayer')
-rw-r--r-- | include/dblayer/functions_mysql.inc.php | 70 | ||||
-rw-r--r-- | include/dblayer/functions_mysqli.inc.php | 70 |
2 files changed, 140 insertions, 0 deletions
diff --git a/include/dblayer/functions_mysql.inc.php b/include/dblayer/functions_mysql.inc.php index c90a5504d..a1853b223 100644 --- a/include/dblayer/functions_mysql.inc.php +++ b/include/dblayer/functions_mysql.inc.php @@ -760,4 +760,74 @@ function my_error($header, $die) echo("</pre>"); } +/** + * Builds an data array from a SQL query. + * Depending on $key_name and $value_name it can return : + * + * - an array of arrays of all fields (key=null, value=null) + * array( + * array('id'=>1, 'name'=>'DSC8956', ...), + * array('id'=>2, 'name'=>'DSC8957', ...), + * ... + * ) + * + * - an array of a single field (key=null, value='...') + * array('DSC8956', 'DSC8957', ...) + * + * - an associative array of array of all fields (key='...', value=null) + * array( + * 'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...), + * 'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...), + * ... + * ) + * + * - an associative array of a single field (key='...', value='...') + * array( + * 'DSC8956' => 1, + * 'DSC8957' => 2, + * ... + * ) + * + * @since 2.6 + * + * @param string $query + * @param string $key_name + * @param string $value_name + * @return array + */ +function query2array($query, $key_name=null, $value_name=null) +{ + $result = pwg_query($query); + $data = array(); + + if (isset($key_name)) + { + if (isset($value_name)) + { + while ($row = pwg_db_fetch_assoc($result)) + $data[ $row[$key_name] ] = $row[$value_name]; + } + else + { + while ($row = pwg_db_fetch_assoc($result)) + $data[ $row[$key_name] ] = $row; + } + } + else + { + if (isset($value_name)) + { + while ($row = pwg_db_fetch_assoc($result)) + $data[] = $row[$value_name]; + } + else + { + while ($row = pwg_db_fetch_assoc($result)) + $data[] = $row; + } + } + + return $data; +} + ?>
\ No newline at end of file diff --git a/include/dblayer/functions_mysqli.inc.php b/include/dblayer/functions_mysqli.inc.php index 41ada251d..31bf9abf3 100644 --- a/include/dblayer/functions_mysqli.inc.php +++ b/include/dblayer/functions_mysqli.inc.php @@ -799,4 +799,74 @@ function my_error($header, $die) echo("</pre>"); } +/** + * Builds an data array from a SQL query. + * Depending on $key_name and $value_name it can return : + * + * - an array of arrays of all fields (key=null, value=null) + * array( + * array('id'=>1, 'name'=>'DSC8956', ...), + * array('id'=>2, 'name'=>'DSC8957', ...), + * ... + * ) + * + * - an array of a single field (key=null, value='...') + * array('DSC8956', 'DSC8957', ...) + * + * - an associative array of array of all fields (key='...', value=null) + * array( + * 'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...), + * 'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...), + * ... + * ) + * + * - an associative array of a single field (key='...', value='...') + * array( + * 'DSC8956' => 1, + * 'DSC8957' => 2, + * ... + * ) + * + * @since 2.6 + * + * @param string $query + * @param string $key_name + * @param string $value_name + * @return array + */ +function query2array($query, $key_name=null, $value_name=null) +{ + $result = pwg_query($query); + $data = array(); + + if (isset($key_name)) + { + if (isset($value_name)) + { + while ($row = $result->fetch_assoc()) + $data[ $row[$key_name] ] = $row[$value_name]; + } + else + { + while ($row = $result->fetch_assoc()) + $data[ $row[$key_name] ] = $row; + } + } + else + { + if (isset($value_name)) + { + while ($row = $result->fetch_assoc()) + $data[] = $row[$value_name]; + } + else + { + while ($row = $result->fetch_assoc()) + $data[] = $row; + } + } + + return $data; +} + ?>
\ No newline at end of file |