Handling three while loops in PHP MYSQL - php

I'm new to PHP and struck with a while loop. Please throw some lights here.
I'm showing some information related to books vs author vs isbn number.
There are two tables: book and author. The book name and ISBN number comes from book table. The author name comes from author table.
Here is my poor php code(please dont laugh)
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$result1 = mysql_query("SELECT book_name FROM book where status='1'") or die(mysql_error());
$result2 = mysql_query("SELECT FName FROM author") or die(mysql_error());
$result3 = mysql_query("SELECT book_isbn_number FROM book where status='1'") or die(mysql_error());
?>
<table style="margin-top:40px" border="1" width="100%">
<tr style="background-color:#909F51">
<td>Book List</td>
<?php while($row1 = mysql_fetch_array( $result1 )) { ?>
<td><?php echo $row1['book_name']; ?></td>
<?php } ?>
</tr>
<?php while($row2 = mysql_fetch_array( $result2 )) { ?>
<tr>
<td><?php echo $row2['author_name']; ?></td>
<?php while($row3 = mysql_fetch_array( $result3 )) { ?>
<td><?php echo $row3['book_isbn_number']; ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
I'm not able to loop through the third while condition. I know the basic code structure is wrong.
Could someone help me out in getting the result.
Tables:
book
b_id
book_name
isbn_number
status
author
a_id
author_name
Expected Output:
<table width="100%" border="1">
<tr>
<td></td>
<td>Author Name 1</td>
<td>Author Name 2</td>
</tr>
<tr>
<td>Book Name 1</td>
<td>ISBN Number 1</td>
<td>ISBN Number 2</td>
</tr>
<tr>
<td>Book Name 2</td>
<td>ISBN Number 3</td>
<td>ISBN Number 4</td>
</tr>
</table>

You are using $row3['isbn_number'] when the column you are selecting is "book_isbn_number". Try changing that line to be <td><?php echo $row3['book_isbn_number']; ?></td>. The loops themselves, while using the old, deprecated mysql_* functions, should be working fine.

<td><?php echo $row3['isbn_number']; ?></td>
replace it with
<td><?php echo $row3['book_isbn_number']; ?></td>

$result3 = mysql_query("SELECT book_isbn_number FROM book where status='1'") or die(mysql_error());
Should be
$result3 = mysql_query("SELECT isbn_number FROM book where status='1'") or die(mysql_error());**
OR
<td><?php echo $row3['isbn_number']; ?></td>
Should be
<td><?php echo $row3['book_isbn_number']; ?></td>
Whichever fits your need

Related

Can't display the query results [duplicate]

This question already has answers here:
Single Value Mysqli [duplicate]
(8 answers)
Closed 3 years ago.
I can't seems to display the query results on my HTML. It displays nothing.
And I really don't know how to do that.
$qr1= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=1;");
$qr2= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=2;");
$qr3= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=3;");
$qr4= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=4;");
$qr5= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=5;");
$total= $qr1 + $qr2 + $qr3 + $qr4 + $qr5;
My HTML :
<tbody>
<tr>
<th scope="row">QR1</th>
<td><?php echo $qr1 ?></td>
</tr>
<tr>
<th scope="row">QR2</th>
<td><?php echo $qr2 ?></td>
</tr>
<tr>
<th scope="row">QR3</th>
<td><?php echo $qr3 ?></td>
</tr>
<tr>
<th scope="row">QR4</th>
<td><?php echo $qr4 ?></td>
</tr>
<tr>
<th scope="row">QR5</th>
<td><?php echo $qr5 ?></td>
</tr>
</tbody>
what's missing is that you need to specify the column on your $qr.
Here's the shorter version of your code.
$total = 0;
$qr= mysqli_query($conn,"select qr, concat('QR', cast(id as varchar(3))) as id FROM `count` where id in (1,2,3,4,5) order by id;");
<tbody>
while ($row = mysqli_fetch_row($qr)) {
<tr>
<th scope="row">
<?php echo $row['id'] ?>
</th>
<td><?php echo $row['qr'] ?></td>
</tr>
$total = $total + $row['qr'];
}
</tbody>

How to fetch all data in front of a single column in php

I have a problem with my code while fetching data of a patient. I want all data of a patient in front of a patient name. For example, I have 5 rows of a patient, I want the patient to fetch patient name and all-time visits in next column.
________________________________________
| Name | Visits Date | Product |
----------------------------------------
| Ali | 1 May | test |
| | 2 May | test 2 |
| | 3 May | test 3 |
----------------------------------------
Here is my code but not working and showing only last column.
<table class="table" border="1" id="data-table5">
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Product</th>
</tr>
</thead>
<tbody>
<?php
//Fetching Data From DB (Sale)
$queryex = "SELECT * FROM `dep_sale` GROUP BY `c_name`";
$result2 = mysqli_query($link, $queryex);
while ($row = mysqli_fetch_array($result2)) { ?>
<tr>
<td><?php echo $row['c_name']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['frame']; echo ",", $row['size']; echo ",", $row['lense']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Please help me in this situation.Thanks
I think you can use it:
$queryex = "SELECT `c_name` * FROM `dep_sale` GROUP BY `c_name`";
You should use ORDER BY instead of GROUP BY. If you use GROUP BY you will only have one record per 'cname'
<table class="table" border="1" id="data-table5">
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Product</th>
</tr>
</thead>
<tbody>
<?php
//Fetching Data From DB (Sale)
$queryex = "SELECT * FROM `dep_sale` ORDER BY `c_name` ASC";
$result2 = mysqli_query($link, $queryex);
$cname = '';
while ($row = mysqli_fetch_array($result2)) {
// compare values of $row['cname'] with $cname;
$name = ($row['cname'] != $cname)? $row['cname'] : '';
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $row['visit_date']; ?></td>
</tr>
<?php
// update $cname value with $row['cname'] within the while loop
$cname = $row['cname'];
}
?>
Sample output:
<tr>
<td>Ali</td>
<td>1 May</td>
</tr>
<tr>
<td></td>
<td>2 May</td>
</tr>
<tr>
<td></td>
<td>3 May</td>
</tr>
<tr>
<td></td>
<td>4 May</td>
</tr>
<tr>
<td>peter</td>
<td>1 May</td>
</tr>
<tr>
<td></td>
<td>2 May</td>
</tr>
<tr>
<td></td>
<td>3 May</td>
</tr>
<tr>
<td></td>
<td>4 May</td>
</tr>
Run your original queries
"SELECT * FROM `dep_sale` GROUP BY `cname`"
"SELECT * FROM `dep_sale` GROUP BY `cname` ASC"
"SELECT * FROM `dep_sale` GROUP BY `cname` DESC"
with phpMyAdmin separately to see different results

How to create pie chart using PHP and MYSQL?

I have a table which shows my data and it works perfectly. I need to add in a pie chart also to show the exact same data that was shown on the table.
<thead>
<tr>
<th>Account Type</th>
<th>Account Number</th>
<th>Account Balance</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM account WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)) {
$acc_type = $row ['account_type'];
$acc_num = $row ['account_number'];
$acc_bal = $row ['account_balance'];
?>
<tbody>
<tr>
<td><?php echo $acc_type ?></td>
<td><?php echo $acc_num ?></td>
<td>$ <?php echo $acc_bal ?></td>
<?php
}
?>
</tr>
</tbody>
<tr class="alt">
<?php
$query = "SELECT SUM(account_balance) AS account_total FROM account WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)){
$acc_total = $row ['account_total'];
?>
<td colspan="2" align="right"><b>Total: </b></td>
<td align="right"><b>$ <?php echo $acc_total ?> </b></td>
<?php
}
?>
</tr>
Use a 3rd party.
Here is one. Look at their Example #15 - Basic Pie graphs
http://pchart.sourceforge.net/documentation.php?topic=exemple15
Why don't you try the Google graphs, that is the easiest way.
Check out the link
https://developers.google.com/chart/interactive/docs/gallery/piechart
If anyone is still looking for a solution I really just went to this site
https://developers.google.com/chart/interactive/docs/drawing_charts
and echo the whole code and it works.

Wht the php query return a single row

i am developing a off-line chat application, i have two table 1. user details (cli_id,email, User name ) 2. chat table (c_from, c_to, subject, matter, image) now the problem is that i am taking the cli_id from the user table as from and to but when fetching the query it return a single row, my code looks like this
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$sql=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['matter']; ?></td>
<td><?php
$chat_to =$row['c_to'];
$sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
while($qry=mysql_fetch_array($sql))
{
echo $qry['email'];
}
?></td>
</tr>
<?php } ?>
</table>
You're overwriting $sql inside the loop, which replaces the result set in your outer loop with a result set which is already "emptied" by the time the code execution returns to the outer loop.
$sql variable changed inside the while loop. Use a different variable here:
$sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
try this :
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$selectChat=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($selectChat))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['matter']; ?></td>
<td><?php
$chat_to =$row['c_to'];
$selectClient=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
while($qry=mysql_fetch_array($selectClient))
{
echo $qry['email'];
}
?></td>
</tr>
<?php } ?>
</table>
It is probably better for you to use a join in order to minimize the amount of database requests, this will also reduce the need for you to have a second query loop inside the first loop. Try the following code
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$sql=mysql_query("SELECT * FROM `chat` LEFT JOIN 'client' on 'chat.c_to = client.cli_id' ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?> </td>
<td><?php echo $row['matter']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</table>
You must need to rewrite the while statement that appears immediately after the main query
while($row=mysql_fetch_array($sql))
as
while($row=mysql_fetch_row($sql))
Hope this might help you.

Switch mysql query to compare results

I have a table containing several columns, one of which is a date column (data1).
The mysql query used is
SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()
We then take the data from each row, run some calculations and store this as a separate variables.
I would now like to compare this data with data from last week (in the same table), i.e. by changing the SELECT query.
Let me expand...
Query for getting this weeks data from table:
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
Now we run through extracting the data
$result = mysql_query($sqld) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$referred = $referred + $row['referred'];
$invalidated = $invalidated + $row['invalidated'];
$tobequalified = $tobequalified + $row['tobequalified'];
}
(the above is just a snippet of the calculations we need to run to demonstrate).
Now we display the results based on this weeks data
<h4>Totals for this week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
I'd like to now change the $sqld query above to select rows in the table that fall into last week, run the same calculations above and display the results below so we can compare the two.
<h4> Totals for last week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
Is there any way of achieving this without copying everything and changing the $sqld query?
I think you can create a function for repeting data and call it with some parameter to change the query like below
somefunction("Previous");
somefunction();
function somefunciton($query = "current") {
if($query == "Current")
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
else
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 2 WEEK) and (now(),INTERVAL 1 WEEK)";
$result = mysql_query($sqld) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$referred = $referred + $row['referred'];
$invalidated = $invalidated + $row['invalidated'];
$tobequalified = $tobequalified + $row['tobequalified'];
}
<h4>Totals for this week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
}
$result1 = mysql_query("SELECT * FROM leads");
$result2 = mysql_query("SELECT * FROM other table");
$arr1 = new Array();
$arr2 = new Array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr1['FirstName'] = $row['your column name'];
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr2['FirstName'] = $row['your column name'];
}
now you have your two arrays($arr1 and $arr2) and you compare as per you want...

Categories