MYSQL SELECT statement for selecting date period - php

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

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();
?>

php echo result from mysql and datetime selection

MySQL database Start = "2017-03-29 01:30:00"
The problem is:
I print the $SQL and search the record in MySQL database, it can search the record. However, I need to debug and test if there is no result, it will be expected to echo "No". But, it always to echo "Yes" no matter I can get the record in MySQL or not.
How can I fixed it.
Main purpose: Get the record if there are and Echo "No" if there don't have record
<?php
$serverName = "localhost";
$username = "root";
$password = "";
$dbName = "fyp";
$tbName = "events";
$String_start = '2017-03-29 01:28:00';
$String_end = '2017-03-29 01:32:00';
$conn = new mysqli($serverName, $username, $password, $dbName);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$staffID = $_SESSION['userID'];
$staff_ID = '15207800';
$sql = "SELECT *
FROM `$tbName`
WHERE `start` BETWEEN ('$String_start') AND ('$String_end')";
echo $sql;
$result=mysqli_query($conn,$sql);
if($result)
{
echo "Yes";
}
else{
echo "no";
}
?>
Mysql query only return fails if there is an issue, for successful execution it returns TRUE even if there is no record. You can achieve with follwing way
<?php
$serverName = "localhost";
$username = "root";
$password = "";
$dbName = "fyp";
$tbName = "events";
$String_start = '2017-03-29 01:28:00';
$String_end = '2017-03-29 01:32:00';
$conn = new mysqli($serverName, $username, $password, $dbName);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$staffID = $_SESSION['userID'];
$staff_ID = '15207800';
$sql = "SELECT *
FROM `$tbName`
WHERE `start` BETWEEN ('$String_start') AND ('$String_end')";
echo $sql;
$result=mysqli_query($conn,$sql);
$num_rows = mysqli_num_rows($result);
if($num_rows > 0)
{
echo "Yes";
}
else{
echo "no";
}
?>
Use mysqli_num_rows that will return total results returned by the query. If total > 0 then results found else no results.
To fetch rows from MySQL result set, use mysqli_fetch_assoc
$result = mysqli_query($conn,$sql);
// $result contains result set and will return `TRUE` if query was successfully executed
// and will return `FALSE` only in case of error
$total = mysqli_num_rows($result);
if($total > 0)
{
echo "Yes";
// Fetch rows from mysql result set
while($row=mysqli_fetch_assoc($result))
{
print_r($row);
}
}
else
{
echo "no";
}

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..

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

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

Select a value from a table and use it to update a field of the same table

I am getting some time data in a 12 hour format and i want to convert from 12 hour to 24 hour.This is the script i wrote that uses mysqli.
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "qplat";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select the_time from r_data where transaction_type = 'send'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$id = 8980;
while($row = $result->fetch_assoc()) {
$new_id = $id++;
$new_time = $row["the_time"];
echo "the time is: " . $row["the_time"].'<br/>';
$time = date("Hi", strtotime("$new_time"));
$sql2 = "update r_data set 24_hour_time = '$time' where transaction_type = 'send' and id = $new_id";
$conn->query($sql2);
}
} else {
echo "0 results";
}
$conn->close();
?>
The script updates the column 24_hour_time with one time only leaving out all the other rows.
Can this be done using one table or will i have to insert into a different table then move the data back?.
You could solve the problem you are having using codeigniter like this
public function up(){
$query = $this->db->query("select id,the_time from r_data where transaction_type = 'send'");
foreach ($query->result() as $row)
{
$id = $row->id;
$new_time = $row->the_time;
$time = date("Hi", strtotime("$new_time"));
$data = array(
'24_hour_time' => $time
);
$this->db->where('id', $id);
$this->db->update('r_data', $data);
}
}

Categories