getting sum of a column in php is not working - php

i'm trying to get the sum of values in a column inside php but it's not working at all while the query works fine when testing in MySQL here's the code (updated with full code)
require('../_req/base.php');
$getCartQ = "select * from user_product inner join cart inner join products inner join users on user_product.User_ID = cart.User_ID and user_product.User_ID = users.User_ID group by cart.User_ID";
$getCartR = mysql_query($getCartQ);
?> <table align="center" width="1271" border="0" cellpadding="0" cellspacing="0">
<?php
while($cartRow = mysql_fetch_array($getCartR)){
$sumQ = "select SUM(Total) as total from user_product where Status = 'active' and user_product.User_ID = '$cartRow[User_ID]'";
$sumR = mysql_query($sumQ) or die(mysql_error());
$sumRow = mysql_fetch_assoc($sumR);
$cost = $sumRow['total'];
?>
<tr>
<td><?php echo $cartRow['Full_Name'] ?></td>
<td><?php echo $cartRow['State']; ?></td>
<td><?php echo $cost; ?></td>
</tr>
<?php
}
?>
</table>
<?php
mysql_close($connect);
when i try to echo $cost i get nothing just a blank space what's wrong with that code ?

user_product.User_ID = '$getCartR[User_ID]'";
to
user_product.User_ID = '{$getCartR['User_ID']}'";

do
echo mysql_error();
at the end and tell me what it's showing. You'll have your answer right there

I just deleted the cache and everything worked fine i don't know what this has to do with the cache thanks all for your help i appreciate it

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.

left join query of mysql php

I'm working on a project where I have two tables 1 is the Users table and another one in the Department table. here I have given a foreign key connection to the department table to the user's table and checked that is working fine. (getting data of particular foreign key). now my requirement is in a single table I want to display all users along with that the foreign key value also. here.
require_once "database.php";
$result = "SELECT * FROM Users";
// , Department WHERE Department.Department_ID = Users.Dpt_id
$output = mysqli_query($conn, $result);
<?php
while ($single = $output->fetch_assoc()):?>
<tr>
<td><?php echo $single['firstname']; ?></td>
<td><?php echo $single['lastname']; ?></td>
<td><?php echo $single['email']; ?></td>
<td><?php echo $single['phnumber']; ?></td>
<td><?php echo $single['provider']; ?></td>
<td><?php echo $single['location']; ?></td>
<td><?php echo $single['Dpt_id']; ?>
</td>
<td><?php if ($single['Dadmin'] == 1){
echo '<p>Department admin</p>';
}
elseif($single['Superuser'] == 1){
echo '<p>SUPER</p>';
}
else{
echo '<p>USER</p>';
}
?></td>
<?php endwhile ?>
tried code:
$result = "SELECT * FROM Users, Department WHERE
Department.Department_ID = Users.Dpt_id";
$output = mysqli_query($conn, $result);
so here is my code. please help me to get the value from the foreign key.
I think you want this result but i don't know your column name:
SELECT u.*, d.department_name
FROM USERS u
LEFT JOIN Department d
ON u.dpt_id = d.department_id
Try this query:
SELECT u.*, d.department_id
FROM USERS u
INNER JOIN Department d
ON u.dpt_id = d.department_id

Display most recent logs for every user

How to display most recent logs for every user?
My database tables are user_log_id, username, login_date, logout_date, user_id. Can you please help me with the query? Thank you
Heres my code:
<?php
$user_query = $conn->query("select * from user_log order by user_log_id DESC ")or die(mysql_error());
while($row = $user_query->fetch()){
$id = $row['user_log_id'];
?>
<tr>
<td><?php echo $row['login_date']; ?></td>
<td><?php echo $row['logout_date']; ?></td>
<td><?php echo $row['username']; ?></td>
</tr>
<?php } ?>
The MySql query you used is correct for getting recent logs. But if you need to get only for specific users or only for logged in users, you have to add a where clause in the query.
select * from user_log where username=".$row['username']." order by user_log_id DESC

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 run two statements in mysqli_query

I need to run two statements to return the data I need. The below code will return all needed except the count column which can be retrieved from a different table.
How can I run these two statements in the same code to retrieve the count as well?
Here is the code I have:
<?php
$query = "SELECT * from $CLIENTS_DATABASE order by id DESC";
if ($result = mysqli_query($conn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?> </td>
<td><?php echo $row["url"]; ?> </td>
<td><?php echo $row["secret"]; ?> </td>
<td><?php echo $row["count"]; ?> </td>
</tr>
<?php
}
mysqli_free_result($result);
}
?>
The other statement that the count data I need is this:
$query = "SELECT COUNT(*) as count from visits where id_client='$result[id]'"
Thanks for your time and any help you can provide.
Join the two queries.
$query = "SELECT order.*, IFNULL(c.count, 0) AS count
FROM $CLIENTS_DATABASE AS order
LEFT JOIN (SELECT id_client, COUNT(*) AS count
FROM visits
GROUP BY id_client) AS c
ON c.id_client = order.id
ORDER BY order.id DESC";

Categories