How can I use mysqli_fetch_array() twice?
You don't need the while
loop and you don't need to use mysqli_fetch_array()
at all!
You can simply loop on the mysqli_result
object itself many times. It implements Traversable
interface that allows it to be used in foreach
.
//Top row
foreach($db_res as $row) {
echo "<td>". $row['Title'] . "</td>";
}
//leftmost column
foreach($db_res as $row) {
echo "<tr>";
echo "<td>". $row['Title'] . "</td>";
.....
echo "</tr>";
}
However, you should separate your DB logic from your display logic and to achieve this it is best to use fetch_all(MYSQLI_ASSOC)
in your DB logic to retrieve all records into an array.
If you fetch all the data into an array, you can loop that array as many times as you want.
$data = $db_res->fetch_all(MYSQLI_ASSOC);
foreach($data as $row) {
// logic here...
}
Use query twice mysql_fetch_array
$stuff = mysql_query("SELECT * FROM users");
while($s = mysql_fetch_array($stuff)){
# ....
}
// add this line
mysql_data_seek( $stuff, 0 );
while($r = mysql_fetch_array($stuff)){
# ...
}
Should do the trick
Another way is of course to store your result in an array and reuse that
Why does mysqli_fetch_array() return array double the size?
Because it also includes an array filled with columns's positions (i.e: 0, 1, 2, 3 and id, username, password, email). Both id
and 0
hold the same data.
If you only want the string indexes, you can use mysqli_fetch_assoc
(http://php.net/mysqli_fetch_assoc)
double results in my array ( mysql_fetch_array )
From the manual:
mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
By default, mysql_fetch_array
gives both associative and numeric indexes. You don't want this. You can limit it with the second parameter:
$query_result_array = mysql_fetch_array($query_result, MYSQL_NUM); // numeric keys only
$query_result_array = mysql_fetch_array($query_result, MYSQL_ASSOC); // associative keys only
You can also use mysql_fetch_row
to only get numeric keys, or mysql_fetch_assoc
to only get associative keys.
$query_result_array = mysql_fetch_row($query_result); // numeric keys only
$query_result_array = mysql_fetch_assoc($query_result); // associative keys only
mysqli_fetch_array() foreach loop duplicates
By default, mysqli_fetch_array()
returns both an Associative Array ($key => $value
) and a Numeric Array (0 => value
), which is why you're getting duplicates - some of the entries have the column name as the key, the others have a numeric index.
If you want an associative array, you could use:
mysqli_fetch_array($q, MYSQLI_ASSOC);
//or
mysqli_fetch_assoc($q);
Or for a numeric array:
mysqli_fetch_array($q, MYSQLI_NUM);
//or
mysqli_fetch_row($q);
For more info: http://www.php.net/manual/en/mysqli-result.fetch-array.php
How to user fetch_assoc() twice in a single php file?
Replace
while($row = $categoryResult->fetch_assoc()) {
with
foreach($categoryResult as $row)
It does the same but the foreach
approach uses an automatic iterator that will always start from the beginning of the result set.
$categoryResult = $mysqli->query();
// OR
$categoryResult = $stmt->get_result();
// from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
// again, from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
If for some reason, you must use while
loop and the manual approach then you need to ensure that you always reset the internal pointer to the first records before starting your loop. However, manual looping is not recommended. It is easier to make more errors when doing this manually with while
$categoryResult->data_seek(0);
while($row = $categoryResult->fetch_assoc()) {
// ...
}
Related Topics
Fatal Error: Cannot Use Object of Type MySQLi_Result
Read Each Line of Txt File to New Array Element
Can You Store a Function in a PHP Array
How to Access MySQL Result Set Data With a Foreach Loop
PHP Header Redirect Not Working
How to Check If a Word Is Contained in Another String Using PHP
How to Get the Real Url After File_Get_Contents If Redirection Happens
Adding Months to a Date in JavaScript
Switch PHP Versions on Commandline Ubuntu 16.04
Symfony2 Conceptual Issue: General Bundles Vs. Specific Ones
Resize Iframe Height According to Content Height in It
PHP X86 How to Get Filesize of ≫ 2 Gb File Without External Program
Assigning the Return Value of New by Reference Is Deprecated
Strtotime With Different Languages
Is There an Equivalent For Var_Dump (PHP) in JavaScript