From 2d9887993cc2c5dbbc22106c07906220bf33836f Mon Sep 17 00:00:00 2001 From: rvelices Date: Tue, 11 Feb 2014 21:47:44 +0000 Subject: 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 --- include/dblayer/functions_mysql.inc.php | 70 ++++++++++++++++++++++++++++++++ include/dblayer/functions_mysqli.inc.php | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) (limited to 'include/dblayer') 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(""); } +/** + * 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(""); } +/** + * 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 -- cgit v1.2.3