Sorting my guestbook posts on time/date (php/mySQL) - php

I need the posts on my guestbook to be sorted by time ascending.
I have been told that I need to add:
ORDER by datetime
in my code. But I dont know what the correct way to enter this line is.
Here is my code:
<?php
$host = "ZZZ"; // Host name
$username = "ZZZ"; // Mysql username
$password = "ZZZ"; // Mysql password
$db_name = "ZZZ"; // Database name
$tbl_name = "ZZZ"; // Table name
// Create connection
$conn = mysqli_connect($host, $username, $password, $db_name);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM ". $tbl_name ." ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "
<b> Name: ". $row["name"]."<br>
Date Added: : ". date('d-m-Y H:i', $row["datetime"]) ."</b><br><br>
Comment: ". $row["comment"]."<br>
<br>
";
}
} else {
echo "0 results";
}
mysql_close(); //close database
?>

$sql = "SELECT * FROM " . $tbl_name . " ORDER BY datetime ASC";
Also you have mysql_close and your other functions are mysqli

Related

How to compare PHP date with MySQL date

So I have a phpMyAdmin database where I have a database called Events, in there I've got the table named Event_table and I have a field called date, which has a DATE Function used to store the date for events. I want to compare PHP's current date with the Database's date. E.g. Today is 2017-06-23 and this is stored in the database as it is, now I want to compare this date with PHP's date and, if they both match each other, then PHP should echo "Event Today", if it doesn't match then it should echo "No Event".
Can someone please give me fully edited code.
<?php
define ('DB_User','root');
define ('DB_Password','password');
define ('DB_HOST','localhost');
define ('DB_Name','events');
$con = mysqli_connect(DB_HOST, DB_User, DB_Password, DB_Name);
if(!$con){
die('Error Connecting');
}
//Don't this need remove at the end of coding...
echo "Connected successfully";
$date = DATE('y-m-d', strtotime("now"));
$sql = "SELECT date FROM Event_table WHERE date = date(now())";
$Query = mysql_query($sql);
$Row = mysql_fetch_array($Query, MYSQL_ASSOC);
$Compare = $Row[0];
if ($Compare == DATE('y-m-d', strtotime("now"))) {
echo "Yes";
}
else {
echo "No";
}
?>
Here you go!
You should pass the date to your query and then check if there are any results. And you should obtain your $date variable differently. See below.
<?php
# ...
$date = (new \Date())->format('Y-m-d');
$sql = "SELECT * FROM Event_table WHERE date = '$date'";
$Query = mysql_query($sql);
$Row = mysql_fetch_array($Query, MYSQL_ASSOC);
if (count($Row)) {
echo "Yes";
}
else {
echo "No";
}
?>
So Finally I Figured it out so Here is the code if anyone Need's it.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "event";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
date_default_timezone_set("Europe/London");
$compare = date("Y-m-d");
/*echo $compare;*/
$sql = "SELECT date FROM event_table WHERE date = '$compare'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Date: " . $row["date"]. "<br>";
echo '<img src="C:\Apache24\htdocs\Teying\yes_logo.png alt="icon" />';
}
} else {
echo "0 results";
}
$conn->close();
?>

MYSQL SELECT statement for selecting date period

I am trying to retrieve data from a MYSQL database using statement but for some reasons the statement is not working as expected. What could be the reason? The values are date inputs from a form t1 and t2. Date values in the form of 2005-04-06. Could it be a date form issue? ...I am getting "No Contacts to Display" despite having data in the database. Is it syntax error?
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "ub435!";
$dbname = "funtest";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$_SESSION['post-data'] = $_POST;
$t1 = $_SESSION['post-data']['t1'];
$t2 = $_SESSION['post-data']['t2'];
$time1 = mysqli_real_escape_string($conn, $t1);
$time2 = mysqli_real_escape_string($conn, $t2);
$sql = "SELECT DISTINCT msisdn FROM customer WHERE DATE_FORMAT(time_paid, '%Y-%c-%e') BETWEEN ADDDATE('$time1',INTERVAL 0 HOUR) AND ADDDATE('$time2',INTERVAL '23:59' HOUR_MINUTE)";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Number of Recipients: "; echo "$result->num_rows <br> <br>";
// output data of each row
while($row = $result->fetch_assoc()) {
$mobilenumber = $row['msisdn'];
echo "Mobile : " . "$mobilenumber" . "<br>";
}
} else {
echo "No Contacts to Display";
}
$conn->close();
?>
You've missed $:
$sql = "SELECT DISTINCT msisdn FROM customer where time_paid BETWEEN '$time1' AND '$time2')";

PHP script returns no result

I have a script where I try to get the date from my database.
The script needs to show: The date in the database is (date). Click here to continue. When I run the SQL query in phpMyAdmin, the SQL query returns the date. When I run it in my script I get no result.
Here is my script:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "set lc_time_names = 'nl_NL';";
$sql = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->multi_query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
When I run this script I get 0. When I change echo "0"; with echo " " . $row['date'] . ". "; I get a empty page.
What am I doing wrong? How can I fix this?
I had another edit that isn't approved...does this work?
Just splitting your queries/variables into 2 separate ones--only only worry that first value may not persist for you.
Like so:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "set lc_time_names = 'nl_NL'";
$sql2 = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$resultZ = $conn->query($sql);
$result = $conn->query($sql2);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
You are using mysqli_query (which executes exactly ONE query) and thus your script is only ever getting to the set variable statement and ending at the semicolon.
Follow the advice below--the 2nd example shows using multi_query and setting the variable value just like you do in phpMyAdmin.
Try with:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$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 date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
Or use multi_query:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "set lc_time_names = 'nl_NL'; SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->multi_query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
You have formatted the sql script.. So instead of writing $row['date'] just put like this $row[0]
Hope this works for you..

Getting User Data Based on Their Information

This first field is where a web visitor will enter in the 'cardname' hit submit and be directed to another page (dashboard2.php) where only his or her content will appear.
Enter your cardname to access your content<br>
<form action='dashboard2.php'>
<input type='text' name='cardname'/><input type='submit' value='retrieve card'/>
</form>
</body>
The page below is the page that is directed after the user enters in the 'cardname' from the first input field. However, I only want this second page to show the information based on the cardname that was entered. Right now, it shows every single cardname, questionone, answerone from that table.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "flashcards";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT cardname, questionone, answerone FROM cards";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
You have to modify the query to accept a WHERE clause. For instance, WHERE cardname = mysqli_real_escape_string($conn, $_GET['cardname']) (The default method for any form is GET unless you specify method="post".).
You should learn about prepared statements for MySQLi and perhaps consider using PDO, it's really not hard.
It seems that you want to perform a search and not a display all the records.
Usually a search returns records that match a certain field, unless a specific ID or unique value was entered in the search. I'm not sure this is the case.
I put this together a little quick but hopefully it helps...
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "flashcards";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// escape the string to avoid SQL injections
$searchEscaped = $conn->real_escape_string($_POST['cardname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT cardname, questionone, answerone FROM cards WHERE cardname = '$searchEscaped' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($result->num_rows == 1){
// only one result found, show just that
$row = $result->fetch_assoc()
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}else{
// multiple rows found, show them all
while($row = $result->fetch_assoc()) {
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}
}
} else {
echo "0 results";
}
$conn->close();
?>

not able to show all records in my database

i am testing my database by executing some MySQLi statements
in this case : i want to display all the records of 2 specefic rows (name,score)
i checked how to do such thing in PHP , and i did it
problem is , the page is not showing anything at all , (blank empty page)
My code :
<?php
$servername = "sql3.freesqldatabase.com";
$username = "MY USERNAME";
$password = "MY PASSWORD";
$dbname = "MY DBNAME";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,score FROM Scores");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"]. " " . $row["score"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
and , i executed the same query in phpMyAdmin Control Panel , and it worked
What have i done wrong ?
This line
$sql = "SELECT name,score FROM Scores");
Should be
$sql = "SELECT name,score FROM Scores";
This syntax error will cause an error and your environment is likely suppressing errors/warnings.

Categories