Archive for category Tech – Web Development
How to identify a MySQL query problem
Posted by Rui Miguel Feio in MySQL, PHP on July 1st, 2009
Sometimes you happen to have a problem with one of your MySQL queries and you simply can’t seem to understand why.
Well, one thing that helps is to use the mysql_error() function.
This function returns the message of the MySQL error you’re getting.
Example:
$action = “INSERT INTO table (name, address, dob) VALUES (‘$name’, ‘$address’, ‘$dob’)”;
$query = mysql_query($action) or die(mysql_error());
If we happen to get a problem with our MySQL error the script execution will stop (by using the die() function) and the MySQL error message will be returned (by using the mysql_error() function).
PHP Session not being passed from a web site to WordPress
Posted by Rui Miguel Feio in PHP, WordPress on June 30th, 2009
Recently I had this case where a PHP session ($_SESSION) was not being passed from a web site to WordPress.
Basically, WordPress was installed and configured to be part of a web site and there was the need to use Sessions to control the login/logout process of the site.
Although the session_start() had been defined to WordPress, the latter did not recognize the session values and therefore simply ignored if a user was logged on.
After some research and some brainstorming with some great experts on the field, I realized that the problem was in the WordPress URL itself.
The problem was that although the domain was the same, WordPress was suppressing the “www” from the URL which caused the $_SESSION values not to be recognized.
Example:
Site URL: http://www.this-is-an-example.com
WP URL: http://this-is-an-example.com (missing the “www.”)
Solution:
By adding the “www.” to the WordPress domain name the problem was solved:
- Login to WordPress Admin area
- Go to Settings – General
- Add “www.” to the WordPress address (URL) and Blog address (URL).
Work with records older then x days
Posted by Rui Miguel Feio in MySQL on June 29th, 2009
For the sake of this example, let’s assume that:
- we want to work with records older then 60 days
- the table name is ‘table’ (how original!)
- the table field with dates is called date_field
DELETE FROM table WHERE date_field < DATE_SUB(CURDATE(),INTERVAL 60 DAY)
This MySQL command deletes all table rows whose date is older then 60 days from current date – CURDATE() -.
SELECT something FROM table WHERE date_field< DATE_SUB(CURDATE(),INTERVAL 60 DAY)
This MySQL command selects all table rows whose date is older then 60 days from current date – CURDATE() -.
Explanation:
DATE_SUB(date,INTERVAL expr unit) -> Subtracts two dates
CURDATE() -> Returns the current date
unit can be:
- MICROSECOND
- SECOND
- MINUTE
- HOUR
- DAY
- WEEK
- MONTH
- QUARTER
- YEAR
- SECOND_MICROSECOND
- MINUTE_MICROSECOND
- MINUTE_SECOND
- HOUR_MICROSECOND
- HOUR_SECOND
- HOUR_MINUTE
- DAY_MICROSECOND
- DAY_SECOND
- DAY_MINUTE
- DAY_HOUR
- YEAR_MONTH