Show records of a particular year in one page - php

<?php
$hostname = "localhost";
$user = "root";
$password = "";
$dbname = "abcd";
$conn = new mysqli($hostname, $user, $password, $dbname);
$sql="SELECT YEAR(date) as year from events GROUP BY year";
$row2 = mysqli_fetch_array(mysqli_query($conn,$sql));
$result2 = $conn->query($sql);
$eyear=$row2['year'];
$year="2017";
for ($counter = 1; $counter <=5; $counter++){
echo'<p>'.$year.'</p>';
$year--;
}
?>
I want to retrieve records of separate years and display results for a particular year in one page

Related

MySQL sum up a column to a LIMIT and return sum value

I have a database and I'm trying to sum up a column up to a specific point with say, LIMIT, and then return the sum value, but it's not working. This is what I have:
<?php
$servername = "localhost";
$username = "xxxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die ("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT sum(donation_count) FROM (SELECT donation_count FROM users LIMIT 9) AS value_sum";
$result = $conn->query ($sql);
if ($result-> num_rows> 0) {
while ($row = $result->fetch_assoc()) {
echo '<div>'".$row['value_sum']."'</div>';
}
}
?>
SELECT sum(donation_count) as value_sum FROM (SELECT donation_count FROM users LIMIT 9) AS temp

Difference between 2 timestamps in days from MySQL table with user session login

I'm trying to figure this out myself. I understand a few things of coding but I'm nowhere good. Well currently I have a chat website where you can purchase a premium membership, I want to display the amount of days that are left until this membership is expiring for the currently logged in user.
My 2 dates for activation and expiration is stored as timestamps.
This is my try at making this but it did not work:
<?php
$servername = "localhost";
$username = "root";
$password = "pass";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT timestamp_activated, timestamp_expire FROM user_subscriptions WHERE id = :id LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$datetime1 = new DateTime('. $row["timestamp_activated"]. ');
$datetime2 = new DateTime('. $row["timestamp_expire"]. ');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a');
}
} else {
echo "0 results";
}
$conn->close();
?>
You could handle this in your MySQL query by using TIMESTAMPDIFF:
SELECT TIMESTAMPDIFF(DAY, timestamp_activated, timestamp_expire) AS daysRemaining
FROM user_subscriptions
WHERE id = :id
LIMIT 1

Count rows in a SQL group

I have a script that I want to put on a dashboard. The SQL works and groups the items in the while and it is displayed in the dashboard correctly. However I also want the QTY's to show.
So if the SQL is grouping how can I count the rows it returns so I can echo the QTY out in to my table for each row. This is my code.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";
$custid = $row['CustomerID'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT * FROM stock
JOIN hardware ON stock.HardwareID = hardware.HardwareID
WHERE stock.CustomerID = $custid GROUP BY stock.HardwareID";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row1['Hardware']."</td>";
echo "<td></td>";
echo "</tr>";
}
}
?>
So the aim is to have the row
echo "<td></td>";
Have a variable in it that shows the qty for each row that is grouped.
you can use this query, group by query has to select and provide aggregation, here i have provided count, if you require some of appropriate column you can change it accordingly...
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";
$custid = $row['CustomerID'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT stock.Hardwareid, count(*) as CountHardware FROM stock
JOIN hardware ON stock.HardwareID = hardware.HardwareID
WHERE stock.CustomerID = $custid GROUP BY stock.HardwareID";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row1['Hardware']."</td>";
echo "<td></td>";
echo "</tr>";
}
}
?>

how to paginate data fetched from databse

lets say if there are 13 records the latest 5 or from 9-13 in the first page,
from 4-8 in second page and 1-3 in the third page
i've tried this but its for the first page only
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysqli_login";
// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT 5")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
$fileName = $row['name'];
$fileContents = file_get_contents("txt/$fileName");
$poster = $row['submittedby'];
$date = $row['trn_date'];
echo ("posted by :$poster | posted date : $date");
echo ("$fileContents");
}
?>
Your code should be like this
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$current_page = 1; // 1=>refer to the first page, 2=> second page and so on..
if(!empty($_GET['page_no']){
$current_page = $_GET['page_no'];
}
$to = "5"; // it is no of record which you wants to show on each page, you can change it's value as per your need.
$from = ($current_page - 1) * $to;
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT $from , $to")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
$fileName = $row['name'];
$fileContents = file_get_contents("txt/$fileName");
$poster = $row['submittedby'];
$date = $row['trn_date'];
echo ("posted by :$poster | posted date : $date");
echo ("$fileContents");
}
?>
your url should be something like http://localhost/projects/?page_no=1
replace "http://localhost/projects/" with your actual url
Hope this will help!!
Here's an article that discusses this topic. You need to offset your query based on the current page. So it will be LIMIT 5 OFFSET <amount based on the page>.

How to retrieve images from database in php

I have a database which has a table called 'propImages' and there are two columns.- 'pid' and 'location'.
And i have data in the database where multiple images can contained by single pid.
image contains database data
now i want to retrieve images from database according to given pid. there can be more than one image.
All i know it there should be an iteration to retrieve images.
I want to display images in HTML .
can you please show me the way to do it in php?
Thanks in advance guys
This may help you
<?php
include 'inc/database.php';
$conn = new mysqli($servername, $username, $password, $database);
$propid = $_GET['propid'];
$sql = "SELECT * FROM propImages WHERE propid='" . $propid . "';";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img src=" . $row['image'] . ">";
}
}
else {
echo "No results";
}
?>
in the inc/database.php :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "database";
?>
To see how it works try visiting : file.php?propid=22
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "databasename";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
//create sql
$sql = "SELECT * FROM `propImages` where pid='$YOUR_PID'";
$result = mysqli_query($con, $sql);
$row = mysqli_num_rows($result);
//retrive data print here
if($row > 0){
while($col = mysqli_fetch_assoc($result))
{
echo $col['location'];
}
} else {
echo 'no result found.';
}
?>
wish it helps

Categories