note 76439 added to function.mysql-fetch-array

From: Date: Mon, 16 Jul 2007 04:02:44 +0000
Subject: note 76439 added to function.mysql-fetch-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-128606@lists.php.net to get a copy of this message
<?php function mysql_fetch_rowsarr($result, $numass=MYSQL_BOTH) { $i=0; $keys=array_keys(mysql_fetch_array($result, $numass)); mysql_data_seek($result, 0); while ($row = mysql_fetch_array($result, $numass)) { foreach ($keys as $speckey) { $got[$i][$speckey]=$row[$speckey]; } $i++; } return $got; } ?> Ever found it a bit of a pain to use mysql_fetch_array()? I find this function much easier to receive data, and to be in a more accessible manner when accessing various data from several rows. It is practical when more than one row is expected to be returned from a SELECT query. It returns a 2-dimensional array in the form of $arr[number_row_returned][column_desired]. So, if there is some column 'password', and you want the password from the third returned row: <?php $result = mysql_query($query); $arr = mysql_fetch_rowsarr($result); $password = $arr[2]['password']; ?> The function also has the ability to return the columns numerically, associatively, or both, just as mysql_fetch_array() does. It uses MYSQL_NUM, MYSQL_ASSOC, and MYSQL_BOTH (MYSQL_BOTH being the default)- exactly like mysql_fetch_array(). So, if the column 'password' is the 4th column which is returned in the query, then password of the 1st returned row could be accessed like so: <?php $arr = mysql_fetch_rowsarr($result, MYSQL_NUM); $password = $arr[0][3]; ?> I hope you find it useful! -Dan ---- Server IP: 69.147.83.197 Probable Submitter: 74.65.190.219 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-array.php Edit -- https://master.php.net/note/edit/76439 Del: integrated -- https://master.php.net/note/delete/76439/integrated Del: useless -- https://master.php.net/note/delete/76439/useless Del: bad code -- https://master.php.net/note/delete/76439/bad+code Del: spam -- https://master.php.net/note/delete/76439/spam Del: non-english -- https://master.php.net/note/delete/76439/non-english Del: in docs -- https://master.php.net/note/delete/76439/in+docs Del: other reasons-- https://master.php.net/note/delete/76439 Reject -- https://master.php.net/note/reject/76439 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128606) next »