Display most recent logs for every user - php

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

Related

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

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";

How to disable SELECT QUERY from auto-sorting the results

<?php
$db = db_connect();
$SQLSELECT = "SELECT * FROM `order` INNER JOIN tb ON order.pc = tb.pc";
$result_set = mysqli_query($db, $SQLSELECT);
foreach($result_set as $row) {
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['add1']; ?></td>
<td><?php echo $row['add2']; ?></td>
<td><?php echo $row['prov']; ?></td>
<td><?php echo $row['pc']; ?></td>
<td><?php echo $row['tier']; ?></td>
</tr>
<?php
I have this code and it gets data from the database by comparing the 2 tables. So for example the one in order table is 5,6,2,1,4,3. So when the query compares the 2 tables it checks for 5 then 6 then 2 and so on. When the results come out, the results become sorted and the output becomes 1,2,3,4,5,6 but I want to output it in the order how I input it. It is somehow auto sorting. Is it possible to disable that?
Unless you explicitly specify a sort order with the user of ORDER BY. Mysql and other databases does not provide any guarantee about the order in what the data is returned.
If you have a table that hasn't had many deletes and updates, the order is likely the order that you inserted. But no guarantee. So use ORDER BY

insert the last 3 visits for each member

i'm designing a Gym Administration System, each member has a unique number stored in a database, each member should type his number when he entered the gym to make sure if his subscription is ended or not, i already design that page, but i need that page to ( add the last 3 visits for each member ) ? how can i do it ?
i need to overwrite the last 3 visits everytime .
ID's 1,2 and 3 are the last visit for member
$con = mysqli_connect('localhost', 'root', '', 'test') or die('error' . mysqli_errno($con));
$query1 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 1");
while ($row = mysqli_fetch_array($query1)){
?>
<div id="checkIn">
<table>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
<?php
$query2 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 2");
while ($row = mysqli_fetch_array($query2)){
?>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
<?php
$query3 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 3");
while ($row = mysqli_fetch_array($query3)){
?>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
</table>
First create a query to fetch all users than loop through them.
$userQuery = $con->prepare("SELECT user_id FROM users");
$userQuery->execute();
$users = mysqli_fetch_array(userQuery);
foreach($users as $user) {
$visitedQuery = $con->prepare("SELECT timeIn, dateIn FROM checkin WHERE user_id = ? ORDER BY id DESC LIMIT 3");
$visitedQuery->execute(array($user['user_id']));
$lastCheckIn = mysqli_fetch_array($visitedQuery);
foreach($lastCheckIn as $lastCheck) {
echo $lastCheckIn['timeIn']." ".$lastCheckIn['dateIn'];
}
}
You mentioned that each user have unique number.
For example:- User A have 123456 unique number.
In Header.php check the condition that user login or not. like:-
if($isset($_SESSION['user_login'])) //you can set your condition here according to your code
{
// first create a new table where user's visit will save.
// Say user database table name is USER_LOGS.which have 3 column.
// ID,User_unique_id,viewpage
$query1 = mysqli_query($con, "SELECT Id,viewpage FROM USER_LOGS WHERE User_unique_id = 12345 ORDER BY Id DESC LIMIT 0"); // that will give you the user's last log.
$total_rows = mysqli_num_rows($query1);
$current_page = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if($total_rows > 0)
{
$row = mysqli_fetch_array($query1);
if($row['viewpage'] != $current_page)
{
mysqli_query($con,"INSERT INTO USER_LOGS (Id,User_unique_id,viewpage)
VALUES ('','123456','".$current_page."')");
}
}
else
{
mysqli_query($con,"INSERT INTO USER_LOGS (Id,User_unique_id,viewpage)
VALUES ('','123456','".$current_page."')");
}
}
The above code is for inert the user log into the database now you can fetch the last three visit by using this query.
mysqli_query($con, "SELECT Id,viewpage FROM USER_LOGS WHERE User_unique_id = 12345 ORDER BY Id DESC LIMIT 0,3");

Categories