Similar Queries different results, why? - php

This one is a bit perplexing since it works perfectly on some records, and not on others. I am trying to pull all records with a specific email and only getting one record in about half of the cases. I have two query sets one pulling the data one is pulling it half of the time.
The first that works all the time is:
$tiera = mysql_query("SELECT SUM(datamb) AS tiera FROM maindata2 WHERE dataplan = '2' and email='".mysql_real_escape_string($_POST['email'])."'");
$tiera1 = mysql_fetch_assoc($tiera);
$tiera2 = $tiera1['tiera'];
$tieradata = mysql_query("SELECT SUM(dataplan) as datatotal FROM maindata2 WHERE dataplan = '2' and email='".mysql_real_escape_string($_POST['email'])."'");
$tieradata1 = mysql_fetch_assoc($tieradata);
$tieradata2 = $tieradata1['datatotal'];
echo "<table id='display1'>
<tr>
<th>Tier:</th>
<th>Total Data in Tier:</th>
<th>Data Usage This Period:</th>
<th>Remaining:</th>
</tr>";
echo "<tr>";
echo "<td>A</td> ";
echo "<td>". $tieradata2 . " MB</td> ";
echo "<td>". $tiera2. " MB</td> ";
echo "<td>".($tieradata1['datatotal'] - ROUND ($tiera1['tiera'],2)) . "MB</td></font> ";
echo "</table>";
I cant use this for my second query because I need it in the loop like this:
$sql = "SELECT phonenumber,date,email, dataplan AS currentplan, SUM(datamb) AS value_sum FROM maindata2 WHERE email='".mysql_real_escape_string($_POST['email'])."' GROUP BY dataplan, phonenumber ";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No Data Found For This Email";
exit;
}
$row = mysql_fetch_assoc($result);
$query = mysql_query($sql) or die(mysql_error());
$header_printed = false;
while($row = mysql_fetch_array($query)) {
if ($row['phonenumber']) {
if ($header_printed === false) {
echo "
<table id='display'>
<tr>
<th>Phone Number:</th>
<th>Data Plan:</th>
<th>Data Usage This Period:</th>
<th>Remaining:</th>
<th>Date Reporting:</th>
</tr>";
$header_printed = true;
}
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "MB</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td><font color=$color>".($row['currentplan'] - ROUND ($row["value_sum"],2)) . "MB</td></font> ";
echo "<td>".$row['date'] . "</td></tr>";
}
}
echo "</table>";
So the question is, what is missing from the second query that is in the first?

Related

creating table rowspan php

i need help with my code..how do i have output like in the image below where it will automatically insert rowspan ..my current code
$result = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("departments")."");
echo "<br /><br /><table class='table table-bordered table-striped'>
<tr>
<th>Bil</th>
<th>Department</th>
<th>Staff</th>
</tr>";
$count =1 ;
while($row = $cmsDB->fetchArray($result))
{
$deptid=$row['deptid'];
$deptname=$row['deptname'];
echo "<tr><td>".$count++."</td><td>".$deptname."</td><td>";
global $cmsDB;
$result2 = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("staff")." WHERE deptid=$deptid");
while($row = $cmsDB->fetchArray($result2))
{
$name=$row['name'];
echo "".$name." <br />";
}
}
echo "</td></tr></table>";
current output and desired results
Please Note: By design performing two queries for this just a bad idea. Still going with that. All you need to do is to perform the second query ahead of creating the block. Record the counts of the result Data and use that as rowSpan. Quite Simple.
global $myDB;
$result2 = $myDB->query("SELECT * FROM ".$myDB->prefix("staff")." WHERE deptid=$deptid");
$rowSpan=$result2-num_rows;
echo "<tr>"
echo "<td rowspan=" . $rowSpan . ">" . $count++ . "</td>";
echo "<td rowspan=" . $rowSpan . ">".$deptname."</td>";
The second while loop must output
<td>$name</td></tr>
The important thing is to close the table row inside the while loop also start a new table row in case you are printing a second name for the same dept.
$result = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("departments")."");
echo "<br /><br /><table class='table table-bordered table-striped'>
<tr>
<th>Bil</th>
<th>Department</th>
<th>Staff</th>
</tr>";
$count =1 ;
while($row = $cmsDB->fetchArray($result))
{
$deptid=$row['deptid'];
$deptname=$row['deptname'];
global $cmsDB;
$result2 = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("staff")." WHERE deptid=$deptid");
$num_rows = $cmsDB->getRowsNum($result2);
$rowSpan=$num_rows;
echo "<tr>";
echo "<td rowspan=" . $rowSpan . ">" . $count++ . "</td>";
echo "<td rowspan=" . $rowSpan . ">".$deptname."</td>";
while($row = $cmsDB->fetchArray($result2))
{
$name=$row['name'];
echo "<td>$name</td></tr>";
}
}
echo "</table>";
I used this logic and it worked perfectly. You can try this.
$result = get_pending_sales_order_details(ST_SALESORDER, $_POST['customer_id'],null);
$order_count=0;
$array_ord=array();
while ($myrow = mysql_fetch_array($result))
{
$result23 = get_pending_sales_order_details(ST_SALESORDER, $_POST['customer_id'],$myrow["order_no"]);
$row_span = mysql_num_rows($result23);
start_row();
if($currentorg != $myrow["order_no"])
{
label_cell($myrow["order_no"], "rowspan='".$row_span."' align=center");
}
label_cell($myrow["stk_code"], "align=center");
label_cell($myrow["description"], "align=left");
label_cell($myrow["pending_quantity"], "align=center");
end_row();
$currentorg = $myrow["order_no"];
}

php - how to exclude a line of code in a loop

I have a code in HTML in a table. And I want the loop to just ignore them
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{
echo "<th>". $row['a']. "</th>";
</thead> // This two line of code
<tbody> // is the one I want to exclude in the while loop
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while($row2 = mysql_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>
Is this even possible?
You need to end your first loop, spit out the html and then start the loop again, havent tested but i think the below should now work.
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while ($row = mysql_fetch_array($rs_admin)) {
echo "<th>" . $row['a'] . "</th>";
}
?>
</thead>
<tbody>
<?php
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while ($row2 = mysql_fetch_array($rs_admin2)) {
echo " <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time'] . "</td>";
echo "</tr>";
}
?>
I'm guessing you want those lines printed once, not to be outside the loop, per se. You could use a variable to track it:
$linesNeeded = true;
while (...) {
...
if ($linesNeeded) {
echo $line1;
echo $line2;
$linesNeeded = false;
}
...
}
Please use mysqli instead of mysql. Take a look: MySQL vs MySQLi when using PHP
+
Your problem's answer too.
<?php
$sel_admin = "query ";
$rs_admin = mysqli_query($connection,$sel_admin);
while($row = mysqli_fetch_array($rs_admin))
{
echo "<th>". $row['a']. "</th>";
?>
</thead>
<tbody>
<?php
$sel_admin2 = "query2 ";
$rs_admin2 = mysqli_query($connection, $sel_admin2);
while($row2 = mysqli_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>
This is honestly just a guess but based on the code you provided you actually need to add more code after remove the code you do not want:
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{
echo "<tr><th>". $row['a']. "</th></tr>"; // Notice the <tr></tr>
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while($row2 = mysql_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>

Displaying "No Results Found" in PHP

Good day!
I am having trouble displaying a "No records found" message in my PHP process.
Here is the code for my search query:
if(isset($_GET['submit'])) {
$product = $_GET['product'];
$city = $_GET['city'];
$query = "SELECT * FROM $product WHERE city = '$city'";
$result = mysqli_query($con, $query) or die ("Could not connect to database.");
$product = str_replace('_', ' ', $product);
$product = strtoupper($product);
echo "You have searched for " . $product . " in " . $city;
echo "<table border=1>";
echo "<tr> <th>Store</th> <th>City</th> </tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>";
echo $row['store'];
echo "</td><td>";
echo $row['city'];
echo "</td></tr>";
}
echo "</table>";
}
My problem is I don't know where and what to place the conditional statement that will show "No records found".
Hoping that someone would be able to help me on this one.
Thanks in advance.
check if the number of rows in the result is 0 via mysqli_num_rows function and display the message before looping over the result set.
your code could be like
if(isset($_GET['submit'])) {
$product = $_GET['product'];
$city = $_GET['city'];
$query = "SELECT * FROM $product WHERE city = '$city'";
$result = mysqli_query($con, $query) or die ("Could not connect to database.");
$product = str_replace('_', ' ', $product);
$product = strtoupper($product);
echo "You have searched for " . $product . " in " . $city;
echo "<table border=1>";
// check if results are present
if(mysqli_num_rows($result)>0) {
echo "<tr> <th>Store</th> <th>City</th> </tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>";
echo $row['store'];
echo "</td><td>";
echo $row['city'];
echo "</td></tr>";
}
} else {
echo "<tr> <td colspan='2'> No Results found </td></tr>";
}
echo "</table>";
}
You should try this code , if any error any code in you code then you can find that.
$result = mysqli_query($con, $query) or die(mysqli_error($con));
OR
if(!$result){
echo die(mysqli_error($result));
}
The mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.
Aslo from reference:http://php.net/manual/tr/mysqli.affected-rows.php
An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error.
$product = $_GET['product'];
$city = $_GET['city'];
$query = "SELECT * FROM $product WHERE city = '$city'";
$result = mysqli_query($con, $query) or die ("Could not connect to database.");
$product = str_replace('_', ' ', $product);
$product = strtoupper($product);
echo "You have searched for " . $product . " in " . $city;
if(mysqli_affected_rows($con) ==0){ echo "No records found"; }
else{
echo "<table border=1>";
echo "<tr> <th>Store</th> <th>City</th> </tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>";
echo $row['store'];
echo "</td><td>";
echo $row['city'];
echo "</td></tr>";
}
echo "</table>";
}

Looping of tables when search button is press

I have problems with looping of my table when displaying here are the codes
<html>
<?php
$Candidate =$_POST ['candidate'];
$link = mysqli_connect('localhost', 'root', '', 'test') or die(mysqli_connect_error());
$query = "SELECT * FROM `table 1` WHERE `fullname` LIKE '$Candidate%'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
mysqli_close($link);
$row=mysqli_fetch_assoc($result);
while ($row = mysqli_fetch_array($result))
{
echo <table>
echo "Name Of Candidate:". #$row['fullname'];
echo "<br>";
echo "comments:".#$row['comments'];
}
?>
initially i want the search results to be displayed in a table format any help?
You can try following
echo "<table>";
while ($row = mysqli_fetch_array($result))
{
echo "<TR><TD>Name Of Candidate:" . $row['fullname'] . "</td>";
echo "<TD>comments:" . $row['comments'] . "</TD></TR>";
}
echo "</table>";
First of all your code is vulnerable to MySQL injection attack. See this SO post
Talking about rendering of the table, the following code should do just fine:
$table = "<table>\n";
$tableHead = <<<THEAD
<thead>\n
<tr>\n
<th>Name of candidate</th>\n
<th>Comments</th>\n
</tr>\n
</thead>\n
THEAD;
//Add table head
$table .= $tableHead;
while ($row = mysqli_fetch_array($result)) {
//No need for # before $row, since your table will have those columns?
$tableRow = <<<TABLEROW
<tr>\n
<td>{$row['fullname']}</td>\n
<td>{$row['comments']}</td>\n
</tr>\n
TABLEROW;
$table .= $tableRow;
}
//Close the table
$table .= "</table>\n";
//Print the table
echo $table;

Display result from database in two columns

EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.

Categories