Acerca de Linux, BSD y notas personales

Archives for PHP mySQL category

Getting the recent one month or year records from MySQL table
Some time we have to collect last 7 or 15 days or X days (or month, year or week) data from MySQL table. For example let us find out who are the new members joined in our forum in last week. One shop may be interested in knowing new products added in last one month. What are the books arrived in last one year. Here irrespective of the date values we want the records of last X days from today, or we can say that the records between today and last X days ( month , year or week) are required.

We will use the MySQL function CURDATE() to get the today’s date.

To get the difference in today date and previous day or month we have to use the MySQL function DATE_SUB

DATE_SUB is a MySQL function which takes date expression, the interval and the constant to return the date value for further calculation.

Here are some sample queries on how to get the records as per requirements . 

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 15 DAY)

The above query will return last 15 days records. Note that this query will return all future dates also. To exclude future dates we have to modify the above command a little by using between query to get records. Here is the modified one.

SELECT * FROM dt_tb WHERE `dt` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 15 DAY ) AND CURDATE( )

Let us try to get records added in last one month

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)

Here also future records will be returned so we can take care of that by using BETWEEN commands if required.

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)

You can easily make out what the above query will return.

Now let us try a different requirement. How to get the records of the working days of the week so far ? If today is Thursday then records from Monday to Thursday should be returned. We will discuss this in our next section >>.

Here is the sql code to create and fill the table with records

CREATE TABLE `dt_tb` ( `id` int(2) NOT NULL auto_increment, `dt` datetime NOT NULL default ‘0000-00-00 00:00:00′, `dt2` date NOT NULL default ‘0000-00-00′, UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

INSERT INTO `dt_tb` VALUES (1, ‘2007-02-15 00:00:00′, ‘2005-01-25′);
INSERT INTO `dt_tb` VALUES (2, ‘2007-02-12 23:56:54′, ‘2005-06-12′);
INSERT INTO `dt_tb` VALUES (3, ‘2005-12-08 13:20:10′, ‘2005-06-06′);
INSERT INTO `dt_tb` VALUES (5, ‘2005-02-10 00:00:00′, ‘2006-01-02′);
INSERT INTO `dt_tb` VALUES (6, ‘2006-11-26 00:00:00′, ‘2006-12-25′);
INSERT INTO `dt_tb` VALUES (7, ‘2006-11-26 00:00:00′, ‘2007-02-25′);
INSERT INTO `dt_tb` VALUES (8, ‘2007-10-20 00:00:00′, ‘2007-10-25′);
INSERT INTO `dt_tb` VALUES (9, ‘2007-02-11 00:00:00′, ‘2007-01-25′);
INSERT INTO `dt_tb` VALUES (10, ‘2007-01-22 00:00:00′, ‘2007-01-15′);

Strings

Concatenar strings

$str1 = “hola “;
$str2 = “que tal “;
$str3= “como estas?”;

$str4 = $str1.$str2.$str3;

Strings Chars
Se pueden reemplazar los caracteres:
$str1 = “0123456789″;
$str1[2]=”x“;

Cambiar a mayusculas y minúsculas:
strtoupper();
strtolower();

Capitaliza la primera letra:
ucfirst();

Capitaliza todas las letras:
ucwords();

ASCII

chr(32) = space
chr(65) = A

ord(“A”) = 65;

 

Tags:

unset()

unset()
Syntax:
    void unset(mixed var [, mixed var, ...])
var
    Variable to unset.
Unsets a variable.
unset() destroys one or more variables and deallocates the memory associated with them.
Code:

<?php

$a = “Some data”;

unset($a);
if (!isset($a)) {
   print “\$a is no longer available”;
}

?>

Output:

$a is no longer available

Introducción a PHP

¿ Que es PHP ?

  • Scripting / Programming Language (“C – Like”)
  • FREE and Open Source
  • Fast, Open, Stable, Cross Platform!
  • No compilation!
  • PHP is all about “Community”

 

 

Comandos utiles mysql

MYSQL HANDY COMMANDS Read more... »

Creating a Database

Those of you who are working on your Web host’s MySQL server have probably already been assigned a database with which to work. Sit tight, we’ll get back to you in a moment. Those of you running a MySQL server that you installed yourselves will need to create your own database. It’s just as easy to create a database as it is to delete one:

mysql>CREATE DATABASE jokes;

I chose to name the database jokes, because that fits with the example we’re using. Feel free to give the database any name you like, though. Those of you working on your Web host’s MySQL server will probably have no choice in what to name your database, since it will usually already have been created for you.

Now that we have a database, we need to tell MySQL that we want to use it. Again, the command isn’t too hard to remember:

mysql>USE jokes;

You’re now ready to use your database. Since a database is empty until you add some tables to it, our first order of business will be to create a table that will hold our jokes.

mysql

mysql

Actualizar todos los campos de una tabla

update mos_mypms_profiles set emailnotify=’1′;