Mysql_Fetch_Array Add All Rows

mysql_fetch_array add all rows?

The most common way:

$rows = array();

while(($row = mysql_fetch_array($result))) {
$rows[] = $row;
}

as shown in the examples in the documentation.

How to add values from mysql_fetch_array?

Try this:

  $result2 = mysql_query ("select sum(totalwithper) from price  where dom='$cat'",$db);
$myrow2= mysql_fetch_array($result2);
do{
echo $myrow2["totalwithper"];
}
while($myrow2=mysql_fetch_array($result2));

a small rewrite to your code :

 $result2 = mysql_query ("select sum(totalwithper) from price  where dom='$cat'",$db);
while($myrow2=mysql_fetch_array($result2))
{
echo $myrow2["totalwithper"];
}

sum by loop

 $sum=0;
$result2 = mysql_query ("select totalwithper from price where dom='$cat'",$db);
while($myrow2=mysql_fetch_array($result2))
{
$sum=$sum+ $myrow2["totalwithper"];
}

echo "sum : $sum";

In $sum you will get the sum.

How to show mysql multi row / mysql_fetch_array results in a table?

First of all I think you dont need 3 different queries for your solution..

    <table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<?php
$result = mysql_query('SELECT name,country,agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
<?php
}
?>
</table>

How to display all rows of mysql_fetch_array using while loop

You are overwriting $product_list in every loop.

You have to concateate by concat operator .=

$product_list .= "$id - $product_name       <a href='#'>edit</a>   • <a href='#'>delete</a> <br/>";

Get all mysql selected rows into an array

I would suggest the use of MySQLi or MySQL PDO for performance and security purposes, but to answer the question:

while($row = mysql_fetch_assoc($result)){
$json[] = $row;
}

echo json_encode($json);

If you switched to MySQLi you could do:

$query = "SELECT * FROM table";
$result = mysqli_query($db, $query);

$json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($json );

mysql_fetch_array does not retrieve all rows

mysql_fetch_assoc returns only one row in once you have to use loop to retrieve all rows

while($row = mysql_fetch_assoc($result))
{
print_r($row);
}

PHP mysql_fetch_array is not returning all rows - one row is always ignored

Every time you call mysql_fetch_array you are taking a row from the resource. When the resource has no more rows to give, it returns false. That's how while ($a = mysql_fetch_array($resource)) loops work.

    $result = mysql_query($query, $connection);
$row = mysql_fetch_array($result) or die(mysql_error());
$totalitems1 = mysql_num_rows($result);

// first row is taken from resource

....

while($row = mysql_fetch_array($result))

// now take the rest of the rows

As you can see, your code is doing exactly what you tell it to! Just remove the first $row = mysql_fetch_array($result) or die(mysql_error()); as it doesn't serve any purpose anyway.

How to show all data one after another from mysql_fetch_array() in php

Simple Solution

You need to get multiple userIds from eventmember table which have multiple users against each event. But you are fetching only once from that query with $row = mysql_fetch_array($resultset);, So you should get only one user, what you are getting now. Hence, the problem is, you actually have put the while loop in a wrong place. The loop should be set like this :

$sql="SELECT userId FROM eventmember WHERE eventid='$event_id';";
$resultset = mysql_query($sql);
$num_row = mysql_num_rows($resultset);
if($num_row) {
while($row = mysql_fetch_array($resultset)) {
$sql22 = "SELECT userId, firstName FROM userinfo WHERE userId='{$row['userId']}';";
$resultset22 = mysql_query($sql22);
$row22 = mysql_fetch_array($resultset22);
$us_id = $row22['userId'];
$us_name = $row22['firstName'];

echo "<tr>";
echo "<td>ID:</td> <td class='text2' align='center' colspan='2'>
<b> $us_id </b>
</u></td>";
echo "</tr>";
//You shouldn't use a break here. This will again give you single result only.
}
}

A Better Solution

Instead of using multiple queries to get the data from userinfo table, use JOIN to get all data with one query. Like this :

$sql="SELECT u.userId,u.firstName FROM eventmember e JOIN userinfo u ON u.userId = e.userId WHERE e.eventid='$event_id';";
$resultset = mysql_query($sql);
$num_row = mysql_num_rows($resultset);
if($num_row) {
while($row = mysql_fetch_array($resultset)) {

$us_id = $row['userId'];
$us_name = $row['firstName'];

echo "<tr>";
echo "<td>ID:</td> <td class='text2' align='center' colspan='2'>
<b> $us_id </b>
</u></td>";
echo "</tr>";
}
}

The Best and Most Secure Solution

As you should have already known mysql_* functions are removed in PHP 7 and this functions are highly harmful for your security. So, you should either move to PDO or mysqli_* functions. I am giving here an example with mysqli_* functions and additionally I am fetching all rows at once instead of doing fetch for each row, which is better for performance.

//First setup your connection by this way.
$link = mysqli_connect(localhost, "my_user", "my_password", "my_db");
//Now you can use mysqli
$sql="SELECT u.userId,u.firstName FROM eventmember e JOIN userinfo u ON u.userId = e.userId WHERE e.eventid=?;";
$stmt = mysqli_prepare($link, $sql);
$stmt->bind_param('s', $event_id);
$stmt->execute();
$resultset = $stmt->get_result();
$resultArray = $resultset->fetch_all();
$num_row = count($resultArray);
if($num_row) {
foreach($resultArray as $row) {

$us_id = $row['userId'];
$us_name = $row['firstName'];

echo "<tr>";
echo "<td>ID:</td> <td class='text2' align='center' colspan='2'>
<b> $us_id </b>
</u></td>";
echo "</tr>";
}
}

How to add all the rows of a column into a variable?

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("streamlist") or die(mysql_error());

$sql = mysql_query("SELECT web_id FROM streams") or die(mysql_error());
while($row = mysql_fetch_assoc($sql)) {

$streamlist[] = $row['web_id'];

}

foreach ($streamlist as $row) {

// Do whatever you want with the data
echo $row.',';

}

$comma_separated = implode(",", $streamlist);


Related Topics



Leave a reply



Submit