note 111852 added to function.mysql-fetch-row
| From: | sarciszewski at knights dot ucf dot edu | Date: | Thu, 04 Apr 2013 15:01:13 +0000 |
| Subject: | note 111852 added to function.mysql-fetch-row | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194339@lists.php.net to get a copy of this message | ||
If you need to quickly just grab all of a particular column (for example, with a "SELECT
DISTINCT a FROM x" query), this quick function will just slice through and fetch an array of
the resultant values:
<?
function mysql_slice($query, $colNo=0) {
// Return an array of single columns
if(gettype($query) == 'string') {
$sql = mysql_query($query) or die(mysql_error());
} else {
$sql = $query;
var_dump($query);
}
$array = array();
for($i = 0; $i < mysql_num_rows($sql); $i++) {
$array[] = mysql_result($sql, $i, $colNo);
}
mysql_free_result($sql);
return $array;
}
?>
To use it:
<?
foreach(mysql_slice("SELECT DISTINCT field FROM table") AS $x) {
echo "{$x}\n";
}
// OR
$array = mysql_slice("SELECT DISTINCT field FROM table");
var_dump($array);
?>
----
Server IP: 69.147.83.199
Probable Submitter: 132.170.99.56
----
Manual Page -- http://php.net/manual/en/function.mysql-fetch-row.php
Edit -- https://master.php.net/note/edit/111852
Del: integrated -- https://master.php.net/note/delete/111852/integrated
Del: useless -- https://master.php.net/note/delete/111852/useless
Del: bad code -- https://master.php.net/note/delete/111852/bad+code
Del: spam -- https://master.php.net/note/delete/111852/spam
Del: non-english -- https://master.php.net/note/delete/111852/non-english
Del: in docs -- https://master.php.net/note/delete/111852/in+docs
Del: other reasons-- https://master.php.net/note/delete/111852
Reject -- https://master.php.net/note/reject/111852
Search -- https://master.php.net/manage/user-notes.php