This question already has answers here:
MySQLi query returns only one row
(3 answers)
Closed 5 years ago.
So I had some mysql code that I've begun to rewrite into mysqli and have run into a problem with the query, and that is when I execute it, I only receive one set of results instead of the several that I know it should be. This is the new code I am using and was wondering whether anyone had any ideas on where I'm going wrong?
code:
<?php
if ($result = $link->query("SELECT SUM(step_count.steps) as total, leagues.league_id, leagues.league_name
FROM step_count INNER JOIN logins on step_count.unique_id = logins.unique_id INNER JOIN leagues ON leagues.unique_id = logins.unique_id GROUP BY leagues.league_id, leagues.league_name ORDER BY `total`
DESC LIMIT 100 ", MYSQLI_USE_RESULT))
$rank = 1; {
$row = $result->fetch_assoc();
$result->close();
}
?>
<tr>
<td>
<?php echo $rank++; ?>
</td>
<td>
<?php echo $row['league_name']; ?>
</td>
<td>
<?php echo $row['total']; ?>
</td>
</tr>
</table>
<?php
mysqli_close($link);
?>
you have to use a while loop
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php } ?>
try like this.
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row->league_name; ?></td>
<td><?php echo $row->total; ?></td>
</tr>
<?php } ?>
you have to put a loop there.
you can replace this code and it will work
while($row =$result->fetch_assoc()){
?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php }
$result->close();
}
?>
Fetch assoc retrieves one row as an associative array.
So you must use a while loop to keep fetching the rows until there are no more. The first example clearly illustrates how. I modified your whole code so you can copy paste everything. Do read the example though.
<?php
$query = "SELECT SUM(step_count.steps) as total,
leagues.league_id, leagues.league_name
FROM step_count
INNER JOIN logins on
step_count.unique_id=logins.unique_id
INNER JOIN leagues ON
leagues.unique_id=logins.unique_id
GROUP BY leagues.league_id, leagues.league_name
ORDER BY `total` DESC LIMIT 100";
$rank = 1;
if ($result = $link->query($query, MYSQLI_USE_RESULT)) {
while($row =$result->fetch_assoc()){
?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php
}?>
</table>
<?php
}
mysqli_close($link);
Related
$query = mysqli_query($conn,SELECT `date`,`name`,`surname`, COUNT(*) AS abc FROM `trial` WHERE `name`='asd' GROUP BY `date`,`name`,`surname` ORDER BY `date` DESC);
<?php while ($row = mysqli_fetch_array($query)) : ?>
<td><?php echo $row['abc']; ?></td>
<td><?php echo $row['Can I print a value returned from a different query, not abc? ']; ?></td>
<?php endwhile; ?>
Can I run a separate query for the second row instead?
Combine them into a single query that gets both counts for each surname.
<?php
$query = mysqli_query($conn,"
SELECT `date`,`surname`, SUM(name = 'asd') AS asd_count, SUM(name = 'opr') AS opr_count
FROM `trial`
WHERE `name`= IN ('asd', 'opr')
GROUP BY `date`,`surname`
ORDER BY `date` DESC");
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['surname']; ?></td>
<td><?php echo $row['asd_count']; ?></td>
<td><?php echo $row['opr_count']; ?></td>
</tr>
<?php endwhile; ?>
See multiple query same table but in different columns mysql
You also need to start a new <tr> for each row returned by the query.
I'm new in PHP with OCI connection. I want to retrieve some data from database and insert it into a table form. But it keep show an error
Trying to get property 'attribute' of non-object
I have try to use oci_fetch object / oci_fetch_array, but it still the same. I also have followed some tutorial but it doesn't help.
This is for my mini project for this semester.
Here my source code:
$sql="SELECT borrow.book_id, book_title, borrow.stud_id, stud_name, book_bdate, return_date, due_date
FROM book
JOIN borrow
ON book.book_id = borrow.book_id
JOIN student
ON borrow.stud_id = student.stud_id
where borrow.stud_id = '$stud_id'
ORDER BY 5 DESC ";
$query=oci_parse($link,$sql) or die ("error here!");
oci_execute($query);
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row->stud_id; ?></td>
<td><?php echo $row->book_id; ?></td>
<td><?php echo $row->book_title; ?></td>
<td><?php echo $row->book_bdate; ?></td>
<td><?php echo $row->due_date; ?></td>
<td><?php echo $row->return_date; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row->book_id; ?>'>Update</a></center>
</td>
<tr>
<?php
}
}
oci_close($link);
?>
Oracle returns field names as uppercase by default so you need to use uppercase indexes like so:
Here the solution where I got.
Btw thank you everyone for helping me.
Correct Your while loop You are fetching data as array:
oci_fetch_array($query, OCI_ASSOC) // return associated array
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row['stud_id']; ?></td>
<td><?php echo $row['book_id']; ?></td>
<td><?php echo $row['book_title']; ?></td>
<td><?php echo $row['book_bdate']; ?></td>
<td><?php echo $row['due_date']; ?></td>
<td><?php echo $row['return_date']; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row['book_id']; ?>'>Update</a></center>
</td>
<tr>
<?php
}
Here's my code sir:
<?php
session_start();
include 'include/db_config.php';
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
?>
<tr class="record">
<td><?php echo $row2['firstname'];?></td>
<td><?php echo $row['no_guest']; ?></td>
<td><?php echo $row['type_service']; ?></td>
<td><?php echo $row['datepicker']; ?></td>
<td><?php echo $row['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
}
?>
i didnt use a LEFT JOIN for displaying data's from 2 tables. i just want to do it in my own way . But my problem is it duplicates my data . before i inserted my second query its just 2 data's and now its 4 already. i just cant figure it out where is the duplication occurs.
Someone help me out please . Thanks in Advance.
You display data in second for, which is inside first for. So you get result count of customer_info table length*service_info length results. You should save info in arrays and then use only one for cycle. Eg.:
<?php
session_start();
include 'include/db_config.php';
$services = array();
$customers = array();
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$services[] = $row;
}
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
$customers[] = $row2;
}
foreach($services as $key => $service) {
?>
<tr class="record">
<td><?php echo $customers[$key]['firstname'];?></td>
<td><?php echo $service['no_guest']; ?></td>
<td><?php echo $service['type_service']; ?></td>
<td><?php echo $service['datepicker']; ?></td>
<td><?php echo $service['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
?>
Btw, i suggest you use different approach, like joins
I've been turning in circles for a while now trying to understand what I have done wrong with lines 68 and 110. It comes up with:
"expects parameter 1 to be resource, boolean given"
but I don't understand where my mistake is. Would someone be able to point me in the right direction or explain my error so I can better understand?
I'm really new to PHP (only started learning it last week) so I've been mostly doing my best with tutorials and what I can find online.
Line 68 onwards:
while($row = mysql_fetch_array($query)){
$card_number = $row['card_number'];
$card_id = $row['card_id'];
$card_name = $row['card_name'];
$card_mana_img = $row['card_mana_img'];
$card_type = $row['card_type'];
$card_rarity = $row['card_rarity'];
$card_set = $row['card_set'];
}
?>
Lines 99 onwards:
<table>
<tr>
<td>Number</td>
<td>Name</td>
<td>Type</td>
<td>Mana</td>
<td>Rarity</td>
<td>Set</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $card_number; ?></td>
<td><?php echo $card_name; ?></td>
<td><?php echo $card_type; ?></td>
<td><?php echo $card_mana_img; ?></td>
<td><?php echo $card_rarity; ?></td>
<td><?php echo $card_set; ?></td>
</tr>
<?php } ?>
</table>
<br>
<?php echo $paginationCtrls; ?><br>
<?php echo $textline2;?><br>
<?php echo $textline1;?>
Don't use this: $sql = "SELECT number FROM magicorigins_cardset";
Use this:
$sql = "SELECT count(*) FROM magicorigins_cardset";
there are multiple column in my table ..
col1 col2 col3 price
500 700 100 10
501 700 100 20
502 700 100 30
503 700 100 10
4 70
I need to get count of col1 and display its sum.
and also display sum of price column...
But the main thing is i need to display this in last row....after all data...how can select this and echo in last row...
plz help me...
I need to echo exactly as i put the data in above table...
I need sql query and also need help to echo only sum of this two column in last row only......
SELECT *,IFNULL(col1,'SUM'), count(*) as count FROM coupon_entry WHERE Group By col1 WITH ROLLUP
<?php if (is_array($data)) { foreach($data as $row){ ?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php } }?>
One solution is to calculate the sum in PHP with variables :
<?php if (is_array($data)) {
$totalCol1 = 0;
$totalPrice = 0;
foreach($data as $row){
$totalCol1 += $row['col1'];
$totalPrice += $row['price'];
?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php }
<tr>
<td><?php echo $totalCol1;?></td>
<td></td>
<td></td>
<td><?php echo $totalPrice;?></td>
</tr>
}?>
Either you have to do 2 separate queries or else you have to do your calculations in PHP - either one is fairly simple although the PHP solution will probably be slightly (negligibly?) faster.
double-query:
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
}
$r2 = query('SELECT COUNT(*) as cnt, sum(col1) as sum_col1,
sum(price) as sum_price
FROM yada-yada...');
if ($row = fetch($r2)) {
echo "<td>$row['cnt']</td><td>$row['sum_col1']</td>...$row['sum_price']...\n";
}
calculate in PHP:
$cnt = $sum_col1 = $sum_price = 0;
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
$cnt++;
$sum_col1 += $row['col1'];
$sum_price += $row['price'];
}
echo "<td>$cnt</td><td>$sum_col1</td>...$sum_price...\n";
Below is my sql query to get count and sum of column.
SELECT COUNT(coupon) As Total,SUM(Price) AS TotalPrice FROM coupon_entry
And put this in seprate row below table...
<?php
foreach($tot as $tota)
?>
<tr style="background-color:#33CCFF;">
<td colspan="2" style="text-align:right; font-weight:bold;">Total Coupon</td>
<td><?php echo $tota['Total'];?></td>
<td style="text-align:right; font-weight:bold;">Total Amount in Rs</td>
<td><?php echo $tota['TotalPrice'];?></td>
</tr>
<?php }?>