mysqli_fetch_array while loop columns
Get all the values from MySQL:
$post = array();
while($row = mysql_fetch_assoc($result))
{
$posts[] = $row;
}
Then, to get each value:
<?php
foreach ($posts as $row)
{
foreach ($row as $element)
{
echo $element."<br>";
}
}
?>
To echo the values. Or get each element from the $post variable
Output of mysqli_fetch_array($data) with loop and without loop
First of all, this code:
while($row=mysqli_fetch_array($data))
{
echo $row['name'];
}
is not equivalent to:
$row=mysqli_fetch_array($data);
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
But rather to something like (pseudocode):
$row = mysqli_fetch_array($data);
if (!$row) return; //break the loop
echo $row['name'];
$row = mysqli_fetch_array($data);
if (!$row) return; //break the loop
echo $row['name'];
...
And you could just use multiple mysqli_fetch_array()
calls to achieve what you need, but there are other ways to achieve it. One of which is the usage of mysqli_fetch_all()
. Both work in a similar way, but mysqli_fetch_all()
returns an array of all results you received from the query, rather than one result at a time.
Now, it returns an array of arrays, so to access it, rather than:
$row['name'];
You would need to use:
$row[ROW_NUMBER]['name']
//example
$row[0]['name']
Examples:
1) mysqli_fetch_array()
$row = mysqli_fetch_array($data);
echo $row['name']; //output name from the first row
$row = mysqli_fetch_array($data);
echo $row['name']; //output name from the second row
2) mysqli_fetch_all()
$row = mysqli_fetch_all($data);
echo $row[0]['name']; //output name from the first row
echo $row[1]['name']; //output name from the second row
Looping inside a while loop mysqli_fetch_array()
let's give it a try!
$count=0;
$arr=array('a', 'b', 'c', 'd');
while ($row = mysqli_fetch_array($query)) {
$img = $row["fn"];
if (empty($img)) { break; }
$thumb = 'images/th_'.$img;
if ($count == 8) { break; }
$i=$arr[$count%4];
$ths.='<div class="ui-block-'.$i.'"><img src="'.$thumb.'"></div>';
$count++;
};
using php for loop for mysqli_fetch_array
There's no reason why you can't use a while
loop. That being said, when you now use sizeof($list)
, you will get the number of columns selected from the query (this $list
variable has as many elements as columns was selected). You will therefor attempt to loop (and ouput the count of) the number of columns you fetched.
Instead, you might want to do something like this below - fetch the number of rows based on mysqli_num_rows()
, which is exactly that - the number of rows returned by the query.
for ($x = 0; $x <= mysqli_num_rows($result); $x++) {
$row = mysqli_fetch_array($result);
var_dump($row);
}
Although, the standard way - and in my opinion the best way - of doing it is via while
.
while ($row = mysqli_fetch_array($result)) {
var_dump($row);
}
mysqli_fetch_array using a foreach loop
Yes! you can just add like this:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
foreach ($row as $key => $value) {
if($key == "contactEmail"){
$mailUrl = "mailto:".$value;
echo "<td><a href=".$mailUrl.">".$value."</a>";
}
else{
echo "<td>" . $value . "</td>";
}
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
Read data from mysqli_fetch_array like a multidimensional array?
while
loop fetches you a row per iteration.
You can get all rows as multidimensional array with mysqli_fetch_all
After that you can use pick your values with [rowNum][colNum]
But beware when your result has lot of rows - array can be very big and cause memory or other issues.
Update to clarify:
if you want to receive multidimensional array of rows there are two ways:
First: iterate over mysqli_result with fetch_assoc
/fetch_array
and append row to array:
$rows = array();
while ($row = mysqli_fetch_array($res)) {
$rows[] = $row;
}
echo $rows[0]['whatever'];
Second: receive all results with one function call:
$rows = mysqli_fetch_all($res, MYSQLI_ASSOC);
echo $rows[0]['whatever'];
That's all. No more methods are available.
mysqli_query, mysqli_fetch_array and while loop
Replace your query with this. Make sure you have added this line before.
$db = mysql_select_db('portal');
$sqlCommand = "SELECT * FROM property";
how to echo specific columns of database using while loop with 3 different tables
You could do using just a single query eg:
SELECT hotels.hotel_name
, room_type.Room_Type_Name
, room.price
, room.facilities
, room.picture
FROM hotels
INNER JOIN room ON room.Hotel_Id=hotels.hotel_id
INNER JOIN room_type ON room.room_type=room_type.Room_Type_Id
ORDER BY hotels.hotel_name , room_type.Room_Type_Name, room.price
Related Topics
Best Way to Completely Destroy a Session - Even If the Browser Is Not Closed
How to Find the First and Last Date in a Month Using PHP
Difference Between 2 Dates in Seconds
How to Create Virtual Host on Xampp
PHP: Sort and Count Instances of Words in a Given String
Including PHP File from Another Server With PHP
How to Detect Strings Like Putjbtghguhjjjanika
In PHP With Pdo, How to Check the Final SQL Parametrized Query
Debugging PHP Mail() And/Or PHPmailer
What Are the Differences Between Composer Update and Composer Install
Running Command-Line Application from PHP as Specific User
Intl Extension: Installing PHP_Intl.Dll
PHP-Intl Installation on Xampp
How to Get First 5 Characters from String
Converting a Simplexml Object to an Array
MySQL_Fetch_Array, MySQL_Fetch_Assoc, MySQL_Fetch_Object