I want to show the array, after array[0] has shown, show next data , loop to do that before have shown all data in database. e.g:
Array (
[0] => Array (
[ID] => 1 [name] => peter [time] = >1:00
)
[1] => Array (
[ID] =>2 [name]=> merry [time]=>3:00
)
...etc
I wish that:
-------------- |id|name |time|
-------------- |1 |peter|1:00|
-------------- |2 |merry|3:00|
-------------- |..|.....|.:..|
but the real is just only show array[0] . like that:
-------------- |id|name |time|
-------------- |1 |peter|1:00|
---------------
Can you tell me how to improve that and correct that?
$sql = "SELECT * FROM mytable";
$result = mysqli_query($conn, $sql);
$datas = array();
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)){
$datas[] = $row;
}
}
$result = $conn->query($sql);
$x = 0;
$a = $datas[$x];
if($a = array())
{
echo "";
}
else
{
foreach($datas[$x] as $data){
echo $data."<td>"."<br>";
$x++;
}
}
There are quite a few issues in your code:
$result = $conn->query($sql) is redundant - you already ran exactly the same query a few lines earlier.
if($a = array()) makes no sense. I think you're probably trying to test whether $a is definitely an array? But what you're really doing is assigning a new empty array to it. It's irrelevant anyway because you never use $a for anything, and we know that $datas will always be an array, so there's no need to check if it's an array. If there are no rows, the loop just won't run.
Writing "<td>"."<br>" will generate invalid HTML. Either use tables and rows, or just use line breaks. You need to make up your mind. Tables is probably better for presenting tabular data...
Most importantly for your question, $datas[$x] only represents the first array of data in $datas. Your loop only loops over the items in that first array. You need to loop over all the indexes in $datas.
Here's a fixed version of your code:
$sql = "SELECT * FROM mytable";
$result = $conn->query($sql);
$datas = array();
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)){
$datas[] = $row;
}
}
echo "<table>"; //start a table
echo "<tr><th>ID</th><th>Name</th><th>Time</th></tr>"; //print the table header
foreach($datas as $row) //loop over all the rows
{
echo "<tr>"; //start a new row
foreach ($row as $field) //loop over the fields in each row.
{
echo "<td>".$field."</td>"; //print a single field in a table cell
}
echo "</tr>"; //end the row
}
echo "</table>"; //end the table
Related
I am having some trouble trying to get this done. The issue is:
I have an array that looks like
wants_arr_flat ( [0] => booze [1] => nudes [2]
=> rooms
I want my foreach loop to go through these values making a different SQL statement and then saving the result of that SQL statement on to another array. The code I am trying to work with now is this.
foreach ( $wants_arr_flat as $value ) {
$sql = "SELECT * FROM offers WHERE user_id != $_SESSION[user_id] AND offer='$value'";
$result=$conn->query($sql);
$want_match_arr = mysqli_fetch_row($result);
echo '<pre>'; print_r($want_match_arr); echo '</pre>'; //testing echo
Obviously this is just overwriting the last array each iteration so I only end up with one result in the array.
instead of
$want_match_arr = mysqli_fetch_row($result);
use
$want_match_arr[] = mysqli_fetch_row($result);
If you get multiple rows from SQL then this is better.
$want_match_arr = [];
foreach ( $wants_arr_flat as $value ) {
$sql = "SELECT * FROM offers WHERE user_id != $_SESSION[user_id] AND offer='$value'";
$result=$conn->query($sql);
while ($row = mysql_fetch_assoc($result)) {
$want_match_arr[] = $row;
}
}
echo '<pre>'; print_r($want_match_arr); echo '</pre>'; //testing echo
I have column name called as email. In that, I have more than 100 rows. Some rows have more than 10 records with a comma(,) some have only 1 records. I have to display all the records in one line.
This is my table
I am getting output like
The output I need in one line so that I can export it.
$sql ="SELECT email FROM email12";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$a=$row['email'];
$b = explode(',',$a);
echo '<pre>'; print_r($b);echo '<pre>';
}
}
I would iterate through exploded array:
$sql ="SELECT email FROM email12";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$a=$row['email'];
$b = explode(',',$a);
foreach($b as $email) {
echo '<pre>'; print_r($email);echo '<pre>';
}
}
}
From the database's perspective, it's not recommended and certainly not a good practice to have data as comma separated list. You should consider normalizing your database. Having said that, you should follow the below procedure to achieve the desired result as of now(or for the time being).
Create an empty array(for example, $resultArr) before the beginning of while loop. In each iteration of while loop, explode email column value and append them to $resultArr array. Finally, after coming out of the loop, simply perform implode operation on the resultant array to display all the records in one line.
$sql ="SELECT email FROM email12";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$resultArr = array();
while($row = $result->fetch_assoc()) {
$resultArr[] = explode(',',$row['email']);
}
echo implode(',', $resultArr);
}
I am trying to fetch a column from a database and display the entire contents as an array. I have so far been able to fetch only one of the entries from the table. I understand that I am fetching only $row[0] which is why i am getting only 1 element. I have tried to fetch all elements but I have not succeeded yet. can someone please let me know how to do it correctly? I have attached my code below.
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT `current` FROM `monitor` ORDER BY `Sno.`";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result,MYSQLI_NUM);
printf("%s\n",$row[0]);
mysqli_close($con)
?>
PDO has a dedicated feature for your problem PDOStatement::fetchColumn.
So, from your question what I understand is- you want to fetch a database column and store this in a array which will make you to display the results.
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT current FROM monitor ORDER BY Sno";
$result = mysqli_query($con,$sql);
$arr = array();
while ($row = mysqli_fetch_array($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
$first_col = array_column($arr, 0);
print_r($first_col);
mysqli_close($con)
?>
i.e. if you have five entry to the database whose first column contains
the values like 'current1', 'current2', 'current3', 'current3' and 'current4' the result of this code will be
Array
(
[0] => current1
[1] => current2
[2] => current3
[3] => current4
[4] => current5
)
I'm trying to run a while loop for each row returned from my query, and at the same time print an array of all the vertical columns values? Here is my code so far, although I'm not sure how to achieve the array.
The thing is I dont want the array of each column value inside the while loop. Basically Im trying to display a html table row of each database row values. So I already have the values displaying in the rows from the while loop, but how to access the vertical column of data and show that as an array in my code to create a graph.
Comments in the code also..
<?php
mysql_select_db("db_name", $con);
$qry = "SELECT * FROM tbl_name WHERE col_name='$col_name'";
$result = mysql_query($qry);
if (!$result) exit("The query didnt succeded");
else {
<!-- my while loop using each individual row from the database -->
while ($row = mysql_fetch_array($result)) {
$col1 = $row['col-name1'];
$col2 = $row['col-name2'];
$col3 = $row['col-name3'];
$col4 = $row['col-name4'];
$col5 = $row['col-name5'];
include 'file_to_be_looped.php';
?>
<br/>
<br/>
<?php
}
}
?>
<!-- then just get a list of all values from a single column of the same query-->
<?php echo $col1 ?> <!--this echo produces the last value in the array, so I think im close-->
Try this:
$col=array();
$i=1;
while ($row = mysql_fetch_array($result)) {
$col[$i] = $row['col-name1'];
$i++;
And later you refer to col1 as $col[1]
EDIT:
maybe I missunderstood :
$col=array(array());
$i=1;
while ($row = mysql_fetch_array($result)) {
$col[&i]['col-name1'] = $row['col-name1'];
$col[$i]['col-name2'] = $row['col-name2'];
$col[$i]['col-name3'] = $row['col-name3'];
$col[$i]['col-name4'] = $row['col-name4'];
$col[$i]['col-name5'] = $row['col-name5'];
$i++;
and later you can refer to them as $col[1]['col-name1']
$column_array = array('col_name1' => array(),
'col_name2' => array(),
'col_name3' => array(),
'col_name4' => array(),
'col_name5' => array());
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $colname => $value) {
$column_array[$colname][] = $value;
}
}
Now if you want to see all the values in a column you can do:
print_r($column_array['col_name1']);
When I query my database with the following in my file, search.php, it only returns the first result it comes across.
$qry = "SELECT business_id FROM business WHERE zip like '%91326%'";
$rs = mysql_query($qry);
$rec = mysql_fetch_array($rs);
echo $session->showContents($rec);
showContents is just a utility function...
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
showContents returned this:
Array
(
[0] => 3
[business_id] => 3
)
The crazy thing is, when I put the same query in sqlbuddy it gives me:
business_id
3
5
6
I am at a loss
mysql_fetch_array fetches only a single row. You want to use it several times to build an array with the entire result set:
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE) {
$rec[] = $row;
}
If you just want the ID's you want to select the ID:
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE) {
$rec[] = $row[0];
}
Try this:
$qry = "SELECT business_id FROM business WHERE zip like '%91326%'";
$rs = mysql_query($qry);
while ($rec = mysql_fetch_array($rs)) {
echo $session->showContents($rec);
}
That's because mysql_fetch_array only fetches a single row from the result set.
Typically you use it like this (from the manual):
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}