Can't display the query results [duplicate] - php

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>

Related

How to hide repeating part of the retrieved data values using PHP , MYSQL & HTML

I have obtained following table as a result of this MySQL statement
select entry.id as id,
entry.payorder,
entry.bank,
entry.PFMS_SCHEME,
entry.name,
entry.vendor,
tally_head.head,
ledgers.amount,
entry.po,
entry.Description,
bank_details.Name as name,
entry.sum as sum
from entry join bank_details on entry.name=bank_details.id
join ledgers on ledgers.id=entry.id
join tally_head on ledgers.ledger=tally_head.id
group by tally_head.head
result i got
These are the mysql tables from which i am retrieving data
entrybank tableledgertally
the final output i want
the view i want
The HTML and PHP Code I used`
<table class="table">
<thead>
<tr>
<th>Voucher No </th>
<th>Payorder </th>
<th>Bank </th>
<th>PFMS_SCHEME </th>
<th>Party Name </th>
<th> For</th>
<th> Po/agmt/inv N.o </th>
<th>Ledger</th>
<th>Ledger Amount</th>
<th>Description/ Narration</th>
<th>sum</th>
</tr>
</thead>
<tbody>
<?php
$sql=mysqli_query($con,"select entry.id as cid,entry.payorder,entry.bank,entry.PFMS_SCHEME,entry.name,entry.vendor,tally_head.head,ledgers.amount,entry.po,entry.Description,bank_details.Name as name,entry.sum as sum from entry join bank_details on entry.name=bank_details.id join ledgers on ledgers.id=entry.id join tally_head on ledgers.ledger=tally_head.id group by tally_head.head ");
$cnt=1;
while($row=mysqli_fetch_array($sql))
?>
<tr>
<td><?php echo htmlentities($row['cid']);?></td>
<td><?php echo htmlentities($row['payorder']);?></td>
<td><?php echo htmlentities($row['bank']);?></td>
<td><?php echo htmlentities($row['PFMS_SCHEME']);?></td>
<td><?php echo htmlentities($row['name']);?></td>
<td><?php echo htmlentities($row['vendor']);?></td>
<td><?php echo htmlentities($row['po']);?></td>
<td><?php echo htmlentities($row['head']);?></td>
<td><?php echo htmlentities($row['amount']);?></td>
<td><?php echo htmlentities($row['Description']);?></td>
<td><?php echo htmlentities($row['sum']);?></td>
<td>
</td>
</tr>
<?php
$cnt++;
?>
</tbody>
</table>

Handling three while loops in PHP MYSQL

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

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.

Submitting Form using Dynamic Text Link

I'm at lost. What I wanted to do is, I want my website to allow the users to select a certain row(records) in my database and then redirect them to another webpage that will show them the full information about that certain record.. I don't know how could I connect those two web pages together using dynamic table/text..
Below is a portion of my code: ( This is the first webpage: )
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_rfqrecord = "SELECT tblrfq.`RFQ_ID`, tblrfq.`Company_Name`, tblrfq.Service,
tblrfq.`Kind_of_Request`, tblrfq.Status, tblrfq.`Date` FROM tblrfq";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
<form id="viewform" name="viewform" method="get" action="ViewSpecificRFQ.php">
<table width="716" border="1" align="center" cellpadding="5">
<tr>
<td>RFQ ID</td>
<td>Company Name</td>
<td>Service</td>
<td>Kind of Request</td>
<td>Status</td>
<td>Date</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
<td><?php echo $row_rfqrecord['Company_Name']; ?></td>
<td><?php echo $row_rfqrecord['Service']; ?></td>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
<td><?php echo $row_rfqrecord['Status']; ?></td>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<?php } while ($row_rfqrecord = mysql_fetch_assoc($rfqrecord)); ?>
</table>
}
</form>
This is the webpage that will get the form..(portion of my code)
$RFQID = $_GET['RFQ_ID'];
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_rfqrecord = "SELECT * FROM tblrfq WHERE $RFQID";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_user = "SELECT tbluser.Username, tbluser.Password FROM tbluser";
$user = mysql_query($query_user, $rfq_portal) or die(mysql_error());
$row_user = mysql_fetch_assoc($user);
$totalRows_user = mysql_num_rows($user);
<table width="716" border="0" align="center">
<tr>
<th colspan="2" scope="row">RFQ ID:</th>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Company Name:</th>
<td width="511"><?php echo $row_rfqrecord['Company_Name']; ?></td>
</tr>
<tr>
<th width="101" rowspan="2" scope="row">Address:</th>
<th width="90" scope="row">Site A:</th>
<td><?php echo $row_rfqrecord['Address_A']; ?></td>
</tr>
<tr>
<th scope="row">Site B:</th>
<td><?php echo $row_rfqrecord['Address_B']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Contact Number:</th>
<td><?php echo $row_rfqrecord['Contact_Number']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Contact Person:</th>
<td><?php echo $row_rfqrecord['Contact_Person']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Service:</th>
<td><?php echo $row_rfqrecord['Service']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Bandwidth:</th>
<td><?php echo $row_rfqrecord['Bandwidth']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Telco:</th>
<td><?php echo $row_rfqrecord['Telco']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Account Manager:</th>
<td><?php echo $row_rfqrecord['Account_Manager']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Status:</th>
<td><?php echo $row_rfqrecord['Status']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Kind of Request:</th>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Date:</th>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Remarks:</th>
<td><?php echo $row_rfqrecord['Remarks']; ?></td>
</tr>
</table>
</form>
this redirects the user to the next page but my problem is it keeps on showing the same record, which is the first record in my database.
1- Consider page1.php is the first page that user selects a record and then in page2.php you show full detail.
for page1.php
you need to send some parameters like record id or any other key in order to be able to identify the record and select the detail in database.
for example:
Record 1 once user clicked on this link you have to do something like this in page2.php, remember our parameter is record_id
<?php
$id = $_GET['record_id'];
//we have to check it for validity
//if id is an integer (numbers) then simply we can check it
//if(!is_numeric($id)) { die("invalid id") }
// now the id is there
//lets build query
$query = "SELECT * from tablename where id= '$id' ";
?>
for your code it must be like this:
//adding where clause
$query_rfqrecord = "SELECT * FROM tblrfq where RFQ_ID = '$id' ";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
If you want your users to get to load a specific record when they click a specific row, you have to be sure you're redirecting them to a specific url. In your sample you have <a href="ViewSpecificRFQ.php"> in your do-while, so every row in your table will have the same link and then the same destination page.
You probably want something like <a href="ViewSpecificRFQ.php?recordId=<?php echo $row_rfqrecord['RFQ_ID'] ?>"> and then in your landing page the query will be performed against the key you will have in $_GET['recordId'].
EDIT: The do-while problem
You use a do-while, and so in your first iteration you have no record loaded, because the fetch instruction is located at the bottom of the code block, so you should change it to:
<?php
while ($row_rfqrecord = mysql_fetch_assoc($rfqrecord)) {
?>
<tr>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
<td><?php echo $row_rfqrecord['Company_Name']; ?></td>
<td><?php echo $row_rfqrecord['Service']; ?></td>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
<td><?php echo $row_rfqrecord['Status']; ?></td>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<?php
}
?>

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