PHP and MySQL syntax: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
Line 43: Line 43:
* [http://www.w3schools.com/php/php_ref_date.asp PHP Date / Time Functions] / [http://www.w3school.com.cn/php/php_ref_date.asp PHP Date / Time 函数]
* [http://www.w3schools.com/php/php_ref_date.asp PHP Date / Time Functions] / [http://www.w3school.com.cn/php/php_ref_date.asp PHP Date / Time 函数]


== Format a number ==
== Math ==
=== Format a number ===
* PHP: [http://php.net/manual/en/function.number-format.php PHP: number_format - Manual]
* PHP: [http://php.net/manual/en/function.number-format.php PHP: number_format - Manual]
* MySQL: LPAD(number,3,0) will output 007 if the value of number is 7
* MySQL: LPAD(number,3,0) will output 007 if the value of number is 7
=== Round a number downward to its nearest integer ===
* [http://php.net/manual/en/function.round.php PHP: round - Manual]
* [http://www.w3schools.com/jsref/jsref_floor.asp JavaScript floor() Method]
* [http://office.microsoft.com/zh-tw/excel-help/HP010062455.aspx ROUND 函數 - Excel - Office.com]


== before delete trigger to make backup of deleted data ==
== before delete trigger to make backup of deleted data ==

Revision as of 10:42, 16 February 2013

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

timestamp

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

now

the human readable time format ex: 2010-12-21 10:05: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: which mapping to DATETIME type[1]
    • SELECT NOW(); /*return the human readable current time: 2011-04-01 12:19:43 */
    • SELECT NOW()+0; //2011-04-01 12:19:43 returns 20110401122023.000000
    • SELECT CURRENT_TIMESTAMP; /*return the human readable current time: 2011-04-01 12:19:43 */

the current timestamp ex: 1292897201

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 ); // convert the time stamp 1292897201 to the human readable time format
  • 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') [2]
  • mysql: SELECT UNIX_TIMESTAMP('2011-03-15 18:53:57'); /* return timestamp: 1300186437 */

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 */

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[3]
  • (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

$array = array("AAA", "BBB", "CCC");

if(in_array($var, $array){
//do something
}
  • preg_match...
  • verbose approach:
if($var =="AAA" OR $var =="BBB" OR $var =="CCC"){
//do something
}

text input filter

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.

references