note 108765 added to function.mysql-fetch-array

From: Date: Wed, 23 May 2012 17:22:14 +0000
Subject: note 108765 added to function.mysql-fetch-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-188168@lists.php.net to get a copy of this message
Just thought I'd share these helper functions that I use to simplify processing of query results a bit: <?php // For a simple query that should return a single value, this returns just that value (or FALSE) so you can process it immediately function db_result_single($result) { return ($row = mysql_fetch_row($result)) && isset($row[0]) ? $row[0] : false; } // Returns the rows as an array of rows // Providing a key_column gives you access to specific rows (e.g. if (isset($result_array[$user_id])) ...) function db_result_array($result, $key_column = null) { for ($array = array(); $row = mysql_fetch_assoc($result); isset($row[$key_column]) ? $array[$row[$key_column]] = $row : $array[] = $row); return $array; } // Returns an array of a single column of data that can optionally be keyed from second column (e.g. an array of user names keyed by user id) function db_result_array_values($result) { for ($array = array(); $row = mysql_fetch_row($result); isset($row[1]) ? $array[$row[1]] = $row[0] : $array[] = $row[0]); return $array; } ?> Naturally, comments are welcome. ---- Server IP: 89.31.146.176 Probable Submitter: 50.135.6.85 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-array.php Edit -- https://master.php.net/note/edit/108765 Del: integrated -- https://master.php.net/note/delete/108765/integrated Del: useless -- https://master.php.net/note/delete/108765/useless Del: bad code -- https://master.php.net/note/delete/108765/bad+code Del: spam -- https://master.php.net/note/delete/108765/spam Del: non-english -- https://master.php.net/note/delete/108765/non-english Del: in docs -- https://master.php.net/note/delete/108765/in+docs Del: other reasons-- https://master.php.net/note/delete/108765 Reject -- https://master.php.net/note/reject/108765 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#188168) next »