sql query to fetch data column wise in php - php

i have created a table with the name of machine in database.
The columns are
edition, start and stop.
From my form, I am inserting multiple start and stop time for single edition.
like wise there are multiple edition.
How to display all data below edition and loop all the rows for single edition and if edition change then it display in another table.
I want to display reports and reports is refreshing after every 30 sec.
<?php
if (isset($_SESSION['city']))
{
$city = make_safe($_SESSION['city']);
}
$sql = "SELECT Distinct * FROM machine where city = '$city' AND DATE(created) = DATE(NOW())";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<tr>
<td class="scheduletime"><?php
echo $row["starttime"];
?></td>
<td class="actualtime"><?php
echo $row["stoptime"];
?></td>
<td class="actualtime"><?php
echo $row["Message"];
?></td>
</tr>
<?php
}
?>

Related

How to display two or more array on same table

Hey guys I have one mysql table tick where i have some info like user id now to display that data I need to retrieve from user table name of the user based on that id
?php
$mysql5 = mysqli_query($dbc, "SELECT * FROM tick where status='1' ORDER BY dt DESC LIMIT 6 ");
if($indtbl = mysqli_fetch_array($mysql5))
{
$title = $indtbl['tickettitle'];
$company = $indtbl['companyname'];
$companyid =$indtbl['compid'];
$trackid = $indtbl['trackid'];
$assignto = $indtbl['assignto'];
$priority = $indtbl['priority'];
}
?>
and user table query is based on first one
<?php
$findresults2323 = mysqli_query($dbc, "SELECT * FROM users WHERE id= '$assignto'");
if($rest = mysqli_fetch_array($findresults2323))
{
$userimg = $rest['img'];
$fname1 = $rest['fname'];
$lname1 = $rest['lname'];
}
?>
now the problem is when i am fetching the array
<thead>
<tr>
<th class="text-left">Title</th>
<th>Company</th>
<th>Ticket ID</th>
<th>Assign To</th>
<th>Priority</th>
</tr>
</thead>
<?php
while($rowten = mysqli_fetch_array($mysql5)) {
$retrive11 = mysqli_fetch_array($findresults23);
?>
<tr>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $rowten["tickettitle"]; ?></td>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $rowten["companyname"];?> <?php echo $rowten["compid"];?></td>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $rowten["trackid"]; ?></td>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $retrive11['fname'];?></td>
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $rowten["priority"]; ?></td>
</tr>
<?php
}
?>
i am getting the error
Notice: Trying to access array offset on value of type null on line 459
line 459 is
<td><a href="readit.php?id=<?php echo $rowten["id"];?>"><?php echo $retrive11['fname'];?></td>
what I am doing wrong
In order to use 2 queries, you need to run the second query inside of the loop so it retrieves the specific user record for the given result from the tick query.
while($rowten = mysqli_fetch_array($mysql5)) {
$assignto = $rowten['assignto'];
$findresults2323 = mysqli_query($dbc, "SELECT * FROM users WHERE id= '$assignto'");
$retrive11 = mysqli_fetch_array($findresults23);
However, this is very inefficient as it requires a query on the user table for every result in the tick query. The proper way of doing this is to use a single query with a join.
SELECT tick.*, users.fname FROM tick LEFT JOIN users ON (tick.assignto=users.id) where tick.status='1' ORDER BY tick.dt DESC LIMIT 6
The result of this query will have all the data from both your queries in one result.

How can I save time to run sql query?

I am a newbie and have very minimum experience to work with php and sql. I have tried the below code but it takes very large amount of time to run.
For each member of the students6 table, his/her Name and Registration number is collected from another table collegestudents and this process takes so much time that server gives
503 error
when students6 table contains huge amount of data. How can the code be changed
to accelerate the process?
$no1=0;
$zq1=mysqli_query($con, "select *
from students6
where CollegeCode='$college'
order by UniversityRollNo");
while($zq2=mysqli_fetch_array($zq1)) {
$xpa=mysqli_query($con, "select *
from collegestudents
where UniversityRollNo='$zq2[UniversityRollNo]'
and CollegeCode='$college'");//
$xp1a=mysqli_fetch_array($xpa);
$name=$xp1a['Name'];
$rgn=$xp1a['RegistrationNo'];
++$no1;
echo "<tr>
<td align='center' >$no1</td><td>$zq2[UniversityRollNo]</td>
<td>$name</td>
<td>$rgn</td>
<td>$zq2[SubjectPaper]</td>
<td align='center'>$zq2[Course]</td>
</tr>";
}
Do both queries in one JOINED query
$no1=0;
$stmt = $con->prepare("select *
from students6 s
JOIN collegestudents c ON c.UniversityRollNo = s.UniversityRollNo
where s.CollegeCode = ?
order by s.UniversityRollNo");
$stmt->bind_param('i',$CollegeCode); //<-- assumed that was holding the code????
$stmt->execute();
$results = $stmt->get_result();
while($zq2 = $results->fetch_assoc){
$name = $xp1a['Name'];
$rgn = $xp1a['RegistrationNo'];
++$no1;
echo "<tr>
<td align='center' >$no1</td><td>$zq2[UniversityRollNo]</td>
<td>$name</td>
<td>$rgn</td>
<td>$zq2[SubjectPaper]</td>
<td align='center'>$zq2[Course]</td>
</tr>";
}

Fetching Specific Orders

This is the query I have written to fetch the records from 3 different tables and display them in one simple table.
$order_id=$_REQUEST['a'];
$query="select DISTINCT product.productId,orders.id,retailerName, total, status, delivery,shopName,ownerName,address,email,contact,notes,order_item.quantity,order_item.date,productName,price,product.detail,product.price,product.expiry from orders,checkout,order_item,product where product.productId=order_item.productId and orders.id ='".$order_id."' and orders.id=order_item.orderId and orders.id=checkout.orderId";
$res=mysqli_query($conn,$query);
$row=mysqli_fetch_array($res);
The below code is written for display of record and detail.
<?PHP
while($row=mysqli_fetch_array($res)){
?>
<tr>
<?php
$subtotal=0;
$subtotal = $row['quantity'] * $row['price'];
$total += $subtotal;
?>
<td><?php echo $row['productName'];?></td>
<td><?php echo $row['quantity'];?></td>
<td><?php echo $row['price'];?></td>
<td><?php echo $subtotal;?></td>
</tr>
<?PHP
}
?>
It displays all of the orders from database whereas I want the orders to be displayed for specific company.
If you have button for show only one result you should link it with some id.
For example:
Delete
Make SQL query show only one result: use WHERE clause.
if(isset($_GET['id'])) {
$id = $_GET['id'];
$q = "DELETE FROM table WHERE id = '$id'";
}
$db->query($q);

How to get data from tables linked to a single user

First of all thank you for help me in advance.
I am looking to make a system were i am using 1 mysql DB and 2 tables or more and connect the tables to a user in a session. So lets suppose that the user logs in in a location, after login he can view from another table the data relative to this user, like a support ticket system (very simple one).
So i created a login system with sessions and then i want to display from another table only the data from that single user but when i do this i get all the data from the table, other users can see other users submissions and i wanna stop that and make it single user, single data display from that user only.
So here is the codes i am using:
<?php
// Validate Client Details
session_start();
if(!isset($_SESSION["user_username"]))
{
header("location:index.php?action=login");
}
?>
Then i got this code to display the records:
<?php
$connect = mysqli_connect("localhost", "root", "root", "helpdesk");
$sql = "SELECT * FROM users INNER JOIN tickets ON users.user_id = tickets.ticket_id";
$result = mysqli_query($connect, $sql);
?>
It works great but displays all and not just the single user details.
The Output i am using:
<table class="table table-striped">
<tr>
<th>Ticket ID</th>
<th>Client Username</th>
<th>Ticket Issue</th>
<th>Message</th>
<th>Priority</th>
</tr>
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["ticket_id"]; ?></td>
<td><?php echo $row["user_username"];?></td>
<td><?php echo $row["ticket_issue"]; ?></td>
<td><?php echo $row["ticket_message"]; ?></td>
<td><?php echo $row["ticket_priority"]; ?></td>
</tr>
<?php
}
}
?>
</table>
I hope someone can help me on this, my best regards and thanks in advance.
You missed where condition
$name=$_SESSION['user_username']
$sql = "SELECT * FROM users INNER JOIN tickets ON users.user_id = tickets.ticket_id where user_username='$name'";

PHP Row and Column Count

I was wondering if you guys can help me, I am pulling Data from a mysql database and I am looking to have the content echo in rows, like a shopping cart. Below is the code I am using. The Code is working flawlessly, but I don't know how to add the Row Count to it and Column Count to it.
Working Code:
<?
$dmealh = mysql_query("select count(*) from jobs_cart where mid='$mid'");
$jmealh = mysql_fetch_array($dmealh);
if ($jmealh[0]) {
$dmealh = mysql_query("select * from jobs_cart where mid='$mid' order by date desc, time desc");
while ($jmealh = mysql_fetch_array($dmealh)) {
echo "$id - $jmealh[name] - $jmealh[meals]";
}
}
?>
This is what I want the Data to look like:
Row Count- Name - Meal Count
1 - Greg - 3
2 - Mike - 4
3 - Tomm - 1
8 Meals Total
Just use a basic counter to keep track of what row you are on and use another variable to add up the meal count as you loop through your results and then echo out the final count. Throw it all into a table and you're set.
<?php
$dmealh = mysql_query("select count(*) from jobs_cart where mid='$mid'");
$jmealh = mysql_fetch_array($dmealh);
if ($jmealh[0]) {
$dmealh = mysql_query("select * from jobs_cart where mid='$mid' order by date desc, time desc");
$total_meals = 0; // Keep track of the total number of meals. Start at zero.
$row = 1; // Keep track of current row. Start at one.
?>
<table>
<?php
while ($jmealh = mysql_fetch_array($dmealh)) {
?>
<tr>
<td><?php echo $row; ?></td>
<td><?php echo $jmealh['name']; ?></td>
<td><?php echo $jmealh['meals']; ?></td>
</tr>
<?php
$total_meals += $jmealh[meals]; // Add to total meals
$row++; // Increase row count
}
?>
<tr>
<td></td>
<td></td>
<td><?php echo $total_meals; ?> Meals Total</td>
</tr>
</table>
}
Don't use mysql_* functions...they're deprecated (note the red box near the top).
Where does $mid come from? Make sure to escape your database inputs to prevent SQL injection.
You're doing extra work by first querying to see if any rows are returned. Just do the SELECT query and while loop. If no rows are returned, the loop will not execute.
You should be using an HTML table to display this data. Use PHP variables to keep track of your sums.
A crude example:
$mid = mysqli_real_escape_string($mid);
$rows = $total = 0;
$result = mysqli_query("SELECT name, meals FROM jobs_cart WHERE mid = '$mid' ORDER BY date DESC, time DESC");
echo '<table>';
while ($row = mysqli_fetch_assoc($result)) {
$rows++;
$total += $row['meals'];
echo '<tr><td>'. $rows .'</td><td>'. $row['name'] .'</td><td>'. $row['meals'] .'</td></tr>';
}
echo '<tr><td> </td><td> </td><td>'. $total .'</td></tr></table>';

Categories