Using the MySQL engine to manipulate values
written by: admin
Date Written: 9/29/13
Last Updated: 10/2/13
In order to do basic MySQL queries you will still need to connect to the database, because the queries still require access to the MySQL engine in order to execute MySQL queries.
The following will work:
<?php include 'dbconnect.php';
$query = "SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');";
$test = mysqli_query($conn,$query);
$test1 = mysqli_fetch_array($test,MYSQL_ASSOC);
$test2 = array_values($test1);
$test3 = $test2[0];
echo "$test3 months.";
?>
The following will not:
<?php
$query = "SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');";
$test = mysqli_query($conn,$query);
$test1 = mysqli_fetch_array($test,MYSQL_ASSOC);
$test2 = array_values($test1);
$test3 = $test2[0];
echo "$test3 months.";
?>
Formatting the date
<?php include 'dbconnect.php';
$query = "SELECT TIMESTAMP('20130919220255'), TIMESTAMP('20100919220251')";
$test=mysqli_query($connect,$query);
$test1=mysqli_fetch_array($test, MYSQLI_NUM);
print_r ($test1);
?>
line 2 formats the string into the datetime data type YYYY–MM–DD HH:MM:SS, so before the time is accepted by the MySQL database MySQL will automatically format it first. This line also shows how it would look if you want to format more than one string.
TAGS: mysql