Display X rows sql using Php - 2D array - php

<?php //sql query which gets the data from database
$query = mysql_query ( "SELECT aaa,bbb,ccc,ddd,eee FROM xyz");
$row = mysql_fetch_row ($query);
?>
This query returns a 2-D array how to display that.
This returns a part of table.

echo $row[0];
echo $row[1];
echo $row[2];
echo $row[3];
echo $row[4];
that's it. Or you can print_r whole array
print_r($row);
but to show all 5 lines you have in your DB, you should do it like
<?php
$query = mysql_query ( "SELECT aaa,bbb,ccc,ddd,eee FROM xyz");
while($row = mysql_fetch_row ($query)){
echo $row[0];
echo $row[1];
echo $row[2];
echo $row[3];
echo $row[4];
}
?>

Related

Output of mysqli_fetch_array($data) with loop and without loop

When I used while loop to print the values of a particular column using mysqli_fetch_array(), I got the correct output from the database.
while($row=mysqli_fetch_array($data))
{
echo $row['name'];
}
But when I used the below mentioned code without the loop, the output was different.
$row=mysqli_fetch_array($data);
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
When I used the below mentioned code, I could see that I get all the rows in the form of an associative array using mysqli_fetch_array().
$con=mysqli_connect("localhost", "root", "", "student_details");
$data=mysqli_query($con,"select * from `registration`");
while ($row = mysqli_fetch_array($data))
{
$rec[]=$row;
}
echo "<pre>";
print_r($rec);
echo "</pre>";
?>
My question is how to get all the values of a particular column (using a while loop), without usage of any loop.
You can use mysqli_fetch_all to get all data.
Check in php docs
-> mysqli_result::fetch_all -- mysqli_fetch_all —
-> Fetches all result rows as an associative array, a numeric array, or both
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
You can use mysqli_fetch_all to get all data in array but you might need to use foreach for further action.
$con=mysqli_connect("localhost", "root", "", "student_details");
$data=mysqli_query($con,"select * from `registration`");
$row = mysqli_fetch_all($data, MYSQLI_ASSOC);
echo "<pre>"; print_r($row); die;
// Will show all data coming in $row as Array
For further processing, you might need to use foreach like below:
foreach ($row as $item) {
print_r($item); // see what your item contains
}

How to display the output in a vertical view dynamically?

I am just running a SQL query to fetch a particular data from the database and would like to display in horizontal way inspite of the vertical format, Can we do something to display the data as required.
For Getting the clear understanding of the question i have attached the Output which i am getting and the output which i required after the SQL query is executed.
Thanks in Advance.
Query:
<?php
include 'connect-db.php';
$query = "SELECT distinct work_component FROM daily_progress_demo WHERE package_no='$package_no'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<center>".$row['work_component']."</center>";
echo "<br />";
}
?>
you can do like this
echo "<table>"
echo "<tr>"
while($row = mysql_fetch_array($result)){
echo "<td>".$row['work_component']."</td>";
}
echo "</tr>";
echo "</table>";
echo $row['work_component']."<br>";

I want to store values in variables then display in table but this wouldn't work

I want to store result first in variables so I could use them but this wouldn't iterate anything. See I tried to store row's values in variables first and called them in td of table but it shows nothing.
Does it have a syntax error or something. What could be the method to store in variables first?
$query_test = "SELECT itemname, categoryname, manufacturername,
price, shopname,itemurl, itemimage, typename
FROM prices p,
items i,
shops s,
categories c,
manufacturers m,
types t,
modules mo
WHERE p.shopid=s.shopid
AND i.categoryid=c.categoryid
AND p.itemid=i.itemid
AND i.ManufacturerId=m.ManufacturerID
AND i.ModuleId= mo.ModuleID
AND i.TypeId=t.TypeID
AND i.categoryid=1004
AND s.shopid=5003";
$result = mysqli_query($conn, $query_test);
echo "<table border='1'>";
echo "<tr><td>"."Name"."</td>";
echo "<td>"."Category"."</td>";
echo "<td>"."Manufacturer"."</td>";
echo "<td>"."Price"."</td>";
echo "<td>"."Shop"."</td>";
echo "<td>"."Type"."</td>";
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
//output a row here
$name = $row['itemname'];
$category = $row['categoryname'];
$manufacturer = $row['manufacturername'];
$price = $row['price']:
$shop = $row['shopname'];
$type = $row['typename'];
echo "<tr><td>".$name."</td>";
echo "<td>".$category."</td>";
echo "<td>".$manufacturer."</td>";
echo "<td>".$price."</td>";
echo "<td>".$shop."</td>";
echo "<td>".$type."</td></tr>";
/*echo "<tr><td>".($row['itemname'])."</td>";
echo "<td>".($row['categoryname'])."</td>";
echo "<td>".($row['manufacturername'])."</td>";
echo "<td>".($row['price'])."</td>";
echo "<td>".($row['shopname'])."</td>";
echo "<td>".($row['typename'])."</td></tr>";*/
}
echo "</table>";
Do a var_dump($row) in your while loop to see what values are stored in $row.
Also, given how you are accessing them, you should probably do:
while( $row = mysqli_fetch_assoc($result))

Echoing details of repeating column values

In my MySQL table I have a column name, and there are various repeating names within the table. If I want to display all other column formation for all the people with name chris I can use:
$sql = mysql_query("SELECT * FROM favorites WHERE name='$name'")
if($info = mysql_fetch_assoc($sql))
echo {$info['name']}
echo {$info['age']}
echo {$info['address']}
But then I want to echo the details of the second chris, in the table who could be in any random row within the table I can't specify that information. Is there a way to accomplish this? Thank you.
Try this:
while($row = mysql_fetch_assoc($sql)) {
echo $row['name'];
echo $info['age'];
echo $row['address'];
}
while ($row = mysql_fetch_assoc($sql))
{
echo $row['name'];
echo $row['age'];
echo $row['address'];
}

loop through database and show in table

I am trying to loop though my users database to show each username in the table in their own row. I can only get it to show one user but loops through this the number of rows there are. Code is below
<?php
require_once ('../login/connection.php');
include ('functions.php');
$query = "SELECT * FROM users";
$results=mysql_query($query);
$row_count=mysql_num_rows($results);
$row_users = mysql_fetch_array($results);
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
?>
Thanks
mysql_fetch_array fetches a single row - you typically use it in a while loop to eat all the rows in the result set, e.g.
echo "<table>";
while ($row_users = mysql_fetch_array($results)) {
//output a row here
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You're only fetching one row:
$row_users = mysql_fetch_array($results);
You're never calling a fetch again so you're not really looping through anything.
I'd suggest changing your loop to the following:
echo "<table>";
while ($row = mysql_fetch_array($results)) {
echo "<tr><td>".($row['email'])."</td></tr>";
}
echo "</table>";
The while loop will loop through the results and assign a row to $row, until you run out of rows. Plus no need to deal with getting the count of results at that point. This is the "usual" way to loop through results from a DB in php.
In the new MYSQLI, I use this coding
$query = "SELECT * FROM users";
$results=mysqli_query($con,$query);
$row_count=mysqli_num_rows($results);
echo "<table>";
while ($row = mysqli_fetch_array($results)) {
echo "<tr><td>".($row['id'])."</td></tr>";
}
echo "</table>";
mysqli_query($con,$query);
mysqli_close($con);
Your problem is in this line:
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
You're echoing out a <table> tag again. Remove that so your code looks like this:
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You don't need to output a table within a table the way you're doing it.
$result = mysql_query("SELECT `email` FROM `users`");
$num_emails = mysql_num_rows($result);
echo "<table><caption>There are/is $num_emails email(s)</caption>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>{$row['email']}</td></tr>";
}
echo '</table>';
Something like that should work.

Categories