Query works in phpMyAdmin but not PHP - php

In phpMyAdmin I have a simple query:
SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1
BUT when I try to do this in my clear_recent.php:
<?php $result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1"); ?>
<?php foreach ($result->fetch_assoc() as $row): ?>
<?php while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I get an error:
Invalid argument supplied for foreach() in /database/chron/clear_recent.php
Cannot for the life of my figure out what's wrong!!!! Please help!

You should check if the query returns any row. try the following code.
<?php
$result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1");
$rows = $result->fetch_assoc();
//check if some rows available.
if (count($rows) > 0) {
foreach ($rows as $row) {
while ($row = $rows) {
?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php
}
}
}
?>
additional question: why you use while loop under the foreach loop? is it right?

You are getting this error because no rows return from this query.

Related

Is there any way to display two data in one row using Rowspan in HTML TABLE?

Im setting up a html table that connected from the database. But I want the table design like the excel which in html table using rowspan. but I cant work out the problem which Im working out.
Please help me to create this format. See the code below
<tbody>
<?php
include_once('connection.php');
$sql = "SELECT * FROM rqn ";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$result = $row['rqn_no'];
?>
<tr>
<td rowspan="2"><?php echo $row['rqn_no'] ?></td>
<td><?php echo $row['date_filed'] ?></td>
<td><?php echo $row['desc_text'] ?></td>
<td><?php echo $row['remarks'] ?></td>
<td><?php echo $row['date_approved'] ?></td>
<td>
<?php
include_once('connection.php');
$sql1 = "SELECT * FROM po_data where po_data.rqn_base = $result ";
$query1 = mysqli_query($conn, $sql1);
while($row1 = mysqli_fetch_assoc($query1)){
?>
<?php echo $row1['po_no'] ?>
<?php
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
Result I Wanted:
https://prnt.sc/md2t2s
Actual Result:https://prnt.sc/md2tpk
You can use rowspan with a div to achieve the result you want. Try this,
<tbody>
<?php
include_once('connection.php');
$sql = "SELECT * FROM rqn ";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$result = $row['rqn_no'];
?>
<tr>
<td rowspan="2"><?php echo $row['rqn_no'] ?></td>
<td><?php echo $row['date_filed'] ?></td>
<td><?php echo $row['desc_text'] ?></td>
<td><?php echo $row['remarks'] ?></td>
<td><?php echo $row['date_approved'] ?></td>
<?php
include_once('connection.php');
$sql1 = "SELECT * FROM po_data where po_data.rqn_base = $result ";
$query1 = mysqli_query($conn, $sql1);
$total_rows = mysqli_num_rows($query1);
// open <td>
echo "<td rowspan=$total_rows>";
while($row1 = mysqli_fetch_assoc($query1)){
?>
<?php echo "<div style='border-bottom:1px solid black'>" . $row1['po_no'] ."</div>" ?>
<?php
}
// close </td>
echo "</td>";
?>
</td>
</tr>
<?php
}
?>
</tbody>
Adjust the CSS according to your need.
Hi this is the final answer to my result.
Output
<tbody>
<?php
include ("connection.php");
$sql = "SELECT * FROM rqn ";
$result= mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($result)):
$ename = $row['rqn_no'];
// count the esal in each ename
$sql2 = "SELECT * FROM po_data WHERE rqn_base=$ename";
$result2 = mysqli_query($conn, $sql2);
$count_result2 = mysqli_num_rows($result2);
?>
<tr >
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['rqn_no']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['date_filed']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['desc_text']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['remarks']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['date_approved']; ?></td>
<?php
// loop each esal
while($row2 = mysqli_fetch_array($result2)):
?>
<td><?php echo $row2['po_no']; ?></td>
</tr>
<?php
endwhile; // endwhile for each esal looping
endwhile; // endwhile for the main looping
?>
</tbody>
Tnx for helping me guys

Calculate total amount of rows for specific date

I have this working.
$queryPR = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'PR')";
I need the total amount of rows calculated for specific date.
I need also to the SUM('pagesat') somehow but I have no idea how.
Find the whole code below.
I am trying to build something simple to show the income and outcome for 3 locations that we have.
<table class="responsive-table">
<thead>
<tr>
<th>Vetura</th>
<th>Targa</th>
<th>Dite</th>
<th>Pagesa</th>
<th>Cmimi Ditore</th>
<th>Vazhdim</th>
<th>Te Tjera</th>
<th>Shpenzime</th>
<th>Komente</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'FR')";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row['vetura']; ?></td>
<td><?php echo $row['targa']; ?></td>
<td><?php echo $row['dite']; ?></td>
<td><?php echo $row['pagesa']; ?>€</td>
<td><?php echo $row['cmimiditore']; ?>€</td>
<td><?php echo $row['vazhdim']; ?>€</td>
<td><?php echo $row['tetjera']; ?>€</td>
<td><?php echo $row['shpenzime']; ?>€</td>
<td><?php echo $row['koment']; ?></td>
</tr>
<?php
$pagesa = $row['pagesa'];
$tetjera = $row['tetjera'];
$vazhdime = $row['vazhdim'];
$shpenzime = $row['shpenzime'];
}?>
</tbody>
</table>
<?php
$totali = ($pagesa + $vazhdime + $tetjera);
$gtotal = ($totali - $shpenzime);
?>
<div class="card grey">
<hgroup class="totalDitorRaport">
<h2>Total</h2>
<h1><?php echo $gtotal; ?>€</h1>
</hgroup>
</div>
try this
get the sum of the row pagesa in the loop
$queryPR = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'PR')";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
$sum = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$sum += $row['pagesa'];
}
echo $sum;

duplicate on my PDO query

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

Issue with show all users in the table with pdo

I have the next issue, when I need the code show me all the users in the table always show me one data the first one or the last one if I change the ASC to DESC inside of SELECT..
I need to show me all users... can you please help me with this?
Here the code and the table with the row I need to show:
<?
include '../include/config.php';
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
$id_paciente = $row['id_paciente'];
$id_tipo = $row['id_tipo'];
$nombre = $row['nombre'];
$apellido = $row['apellido'];
$ciudad = $row['ciudad'];
$telefono = $row['telefono'];
$foto = $row['foto'];
}
?>
<tr>
<th><?php echo $id_paciente; ?></th>
<td><img src="../<?php echo $foto;?>" class="image_thumbnail" /></td>
<td><?php echo $nombre; ?></td>
<td><?php echo $apellido; ?></td>
<td><?php echo $id_tipo; ?></td>
<td><?php echo $ciudad; ?></td>
<td><?php echo $telefono; ?></td>
You are echoing your variables outside of the loop.
So, move it inside:
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
?>
<tr>
<th><?php echo $row['id_paciente'] ?></th>
<td><img src="../<?php echo $row['foto']?>" class="image_thumbnail" /></td>
<td><?php echo $row['nombre'] ?></td>
<td><?php echo $row['apellido'] ?></td>
<td><?php echo $row['id_tipo'] ?></td>
<td><?php echo $row['ciudad'] ?></td>
<td><?php echo $row['telefono'] ?></td>
<tr>
<? } ?>
well I get my answer with my problem...
now I see all the users, the code neccesary is:
<?
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
?>
for somebody want to
Best Regards!

Displaying field wise records from mysql by php?

Hi i have table in mysql like this:
to show this by php like this:
now first i get and print all gradeid on top by this
$qry = "select * from grademaster";
$result= mysql_query($qry,$link);
$nro=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$gradeid = $row['gradeid'];
echo "<th> $gradeid </th>";
$grdid[] = $gradeid; ////////takes all grades in array
}
and then getting all details from the below table
and but i can't able to show records like on top how can i show this plz help
Just copy and paste it instead of your code, then run script in browser.
You'll see the table. Prettify it via CSS and you are done.
I've tested it on my local server and it seems to work.
$qry = "select * from grademaster";
$result = mysql_query($qry, $link);
$nro = mysql_num_rows($result);
$table = array();
$rowNum = 0;
while ($row = mysql_fetch_array($result)) {
$table[$rowNum]['ordid'] = $row['ordid'];
$table[$rowNum]['orddate'] = date('m/d/Y', strtotime($row['orddate']));
$table[$rowNum]['1001'] = intval($row['gradeid']) == 1001 ? $row['ordqty'] : '';
$table[$rowNum]['1002'] = intval($row['gradeid']) == 1002 ? $row['ordqty'] : '';
$table[$rowNum]['1003'] = intval($row['gradeid']) == 1003 ? $row['ordqty'] : '';
$table[$rowNum]['1004'] = intval($row['gradeid']) == 1004 ? $row['ordqty'] : '';
$table[$rowNum]['1005'] = intval($row['gradeid']) == 1005 ? $row['ordqty'] : '';
$rowNum++;
}
?>
<table>
<tr>
<th>ordid</th>
<th>orddate</th>
<th>1001</th>
<th>1002</th>
<th>1003</th>
<th>1004</th>
<th>1005</th>
</tr>
<?php foreach ($table as $row): ?>
<tr>
<td><?php echo $row['ordid']; ?></td>
<td><?php echo $row['orddate']; ?></td>
<td><?php echo $row['1001']; ?></td>
<td><?php echo $row['1002']; ?></td>
<td><?php echo $row['1003']; ?></td>
<td><?php echo $row['1004']; ?></td>
<td><?php echo $row['1005']; ?></td>
</tr>
<?php endforeach; ?>
</table>

Categories