PHP and MySQL syntax: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
Line 9: Line 9:
** {{kbd | key=<nowiki>SELECT NOW();</nowiki>}} /*return the human readable current time: 2011-04-01 12:19:43 */ which mapping to DATETIME type<ref>[http://dev.mysql.com/doc/refman/5.1/en/datetime.html MySQL :: MySQL 5.1 Reference Manual :: 11.3.1 The DATE, DATETIME, and TIMESTAMP Types]</ref>
** {{kbd | key=<nowiki>SELECT NOW();</nowiki>}} /*return the human readable current time: 2011-04-01 12:19:43 */ which mapping to DATETIME type<ref>[http://dev.mysql.com/doc/refman/5.1/en/datetime.html MySQL :: MySQL 5.1 Reference Manual :: 11.3.1 The DATE, DATETIME, and TIMESTAMP Types]</ref>
** {{kbd | key=<nowiki>SELECT NOW()+0;</nowiki>}} //2011-04-01 12:19:43 returns 20110401122023.000000
** {{kbd | key=<nowiki>SELECT NOW()+0;</nowiki>}} //2011-04-01 12:19:43 returns 20110401122023.000000
** {{kbd | key=<nowiki>SELECT CURRENT_TIMESTAMP;</nowiki>}} /*return the human readable current time: 2011-04-01 12:19:43 */
** {{kbd | key=<nowiki>SELECT CURRENT_TIMESTAMP;</nowiki>}} /*return the human readable current time: {{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY2}} 12:19:43 */
** {{kbd | key=<nowiki>SELECT  CURRENT_DATE;</nowiki>}} /* Return {{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY2}} */<ref>[http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_curdate MySQL :: MySQL 5.5 Reference Manual :: 12.7 Date and Time Functions]</ref>
** {{kbd | key=<nowiki>SELECT  CURRENT_DATE;</nowiki>}} or {{kbd | key=<nowiki>SELECT  CURDATE();</nowiki>}}  /* Return {{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY2}} */<ref>[http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_curdate MySQL :: MySQL 5.5 Reference Manual :: 12.7 Date and Time Functions]</ref>
* EXCEL: {{kbd | key=<nowiki>=TODAY()</nowiki>}} Returns the current date. ex: {{CURRENTYEAR}}/{{CURRENTMONTH}}/{{CURRENTDAY2}} <ref>[http://office.microsoft.com/en-gb/excel-help/today-function-HP010062297.aspx TODAY function - Excel - Office.com]</ref>
* EXCEL: {{kbd | key=<nowiki>=TODAY()</nowiki>}} Returns the current date. ex: {{CURRENTYEAR}}/{{CURRENTMONTH}}/{{CURRENTDAY2}} <ref>[http://office.microsoft.com/en-gb/excel-help/today-function-HP010062297.aspx TODAY function - Excel - Office.com]</ref>



Revision as of 12:16, 30 April 2015

mulitple approaches to completing the same/similar task using PHP and MySQL syntax

time / timestamp

Convert time stamp <--> the human readable time format

now

the human readable time format ex: 2024-05-15 16:51:06

  • php: echo date("Y-m-d H:i:s", time() ); //Convert the time stamp of current time to the human readable time format. Ex: return '2010-12-21 10:05:06'
  • mysql:
    • SELECT NOW(); /*return the human readable current time: 2011-04-01 12:19:43 */ which mapping to DATETIME type[1]
    • SELECT NOW()+0; //2011-04-01 12:19:43 returns 20110401122023.000000
    • SELECT CURRENT_TIMESTAMP; /*return the human readable current time: 2024-05-15 12:19:43 */
    • SELECT CURRENT_DATE; or SELECT CURDATE(); /* Return 2024-05-15 */[2]
  • EXCEL: =TODAY() Returns the current date. ex: 2024/05/15 [3]

the current timestamp ex: 1292897201

  • php: echo time(); //Return the number of seconds since 1970/01/01
  • php: echo microtime(); PHP: microtime - Manual "Returns current Unix timestamp with microseconds" //ex: 0.45920500 1406776901
  • mysql: SELECT UNIX_TIMESTAMP(); //Return the number of seconds since 1970/01/01 ex: 2011-05-30 01:56:38 returns 1306720622
  • javascript: getTime() Method ex: new Date().getTime(); //Return the number of milliseconds since 1970/01/01 [4]
  • EXCEL: Excel date to Unix timestamp - Stack Overflow

compare the timestamp and human readable time format using MySQL

SELECT UNIX_TIMESTAMP( ) , FROM_UNIXTIME( UNIX_TIMESTAMP( ) ,  '%Y-%m-%d %H:%i:%S' );

specified time

the human readable time format ex: 2010-12-21 10:05:06

  • php: echo date("Y-m-d H:i:s", 1292897201 ); // Using date() function that converting the time stamp 1292897201 to the human readable time format. similar: gmdate() function [Last visited: 2014-04-30]
  • mysql:
    • SELECT FROM_UNIXTIME( 1306311155 ); //convert the time stamp 1306311155 to the human readable time format 2011-05-25 08:12:11
    • SELECT FROM_UNIXTIME( 1306311155, '%Y-%m-%d %H:%i:%S' ); //convert the time stamp 1306311155 to the human readable time format 2011-05-25 08:12:11


convert human-readable time to timestamp

  • php: using strtotime() function ex: strtotime('2010-12-21 10:05:06') [5]
  • mysql: SELECT UNIX_TIMESTAMP('2011-03-15 18:53:57'); /* return timestamp: 1300186437 */

PHP microtime to MySQL timestamp

PHP microtime to MySQL timestamp

time difference of two time values

  • php: using mktime() function
  • mysql: SELECT TIMEDIFF('2010:01:01 00:00:00', '2010:02:01 00:00:00'); /* return -744:00:00 */

Query by month

  • mysql ex: find the records in January, 2014[6] Data type of column `time` is datetime. Using one of sql queries as following. online demo
    • SELECT * FROM `table_name` WHERE YEAR(`time`) = '2014' AND MONTH(`time`) = '1' /*MONTH(`time`) = '1' or MONTH(`time`) = '01' are both ok*/
    • SELECT * FROM `table_name` WHERE DATE_FORMAT(`time`, '%Y-%m') = '2014-01'
    • SELECT * FROM `table_name` WHERE `time` LIKE '2014-01%' /*find the time records starting with the string 2014-01 */
    • SELECT * FROM `table_name` WHERE date(`time`) between '2014-01-01' AND '2014-01-31'
    • SELECT * FROM `table_name` WHERE `time` >= '2014-01-01 00:00:00' AND `time` < '2014-02-01 00:00:00'
    • SELECT * FROM `table_name` WHERE `time` >= '2014-01-01 00:00:00' AND `time` <= '2014-01-31 00:00:00'

date format

sample output: 2024-05-15

further reading

Math

Format a number

Round a number downward to its nearest integer

before delete trigger to make backup of deleted data

MySQL :: Re: before delete trigger to make backup of deleted data

step 1: create table hello_deleted_data with the same structure of table hello

step 2: add a trigger

create trigger hello before delete on hello 
for each row 
insert into hello_deleted_data (x,y,z) 
values (OLD.x, OLD.y, OLD.z); 

tested on version: mysqlnd 5.0.7-dev - 091210

redirect/reload to (another) page

redirect


reload

  • Javascript
    • iframe (reload) - parent.location.reload();

find unique (non duplicated) data

  • SELECT DISTINCT column FROM table[7]
  • (1)SELECT * FROM table; (2)Using PHP: array_unique for data handling two or more columns
  • SELECT column FROM table GROUP BY column

text match

//PHP code snippet:
$array = array("AAA", "BBB", "CCC");

if(in_array($var, $array){
//do something
}
  • preg_match...
  • verbose approach: Icon_exclaim.gif IF (0 == 'String') always be true?[8]
if($var =="AAA" OR $var =="BBB" OR $var =="CCC"){
//do something
}

text input filter

export/import sql file

export/import sql file


reverse the boolean value

  • PHP:
$var = true;
print( !$var); //add ! symbol to reverse the boolean value

ideas

filter some records

  • approach 1: (1) select * under some condition, (2) collect the $identifiers which matched the condition. and then (3) select some records NOT IN ( $identifiers )
  • approach 2: (1) select * and then (2) unset some records which matched the condition.

debug

SQL syntax debug

references