Archive for category Tech – Web Development

How to identify a MySQL query problem

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).

  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

No Comments

PHP Session not being passed from a web site to WordPress

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:

  1. Login to WordPress Admin area
  2. Go to SettingsGeneral
  3. Add “www.” to the WordPress address (URL) and Blog address (URL).
  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

11 Comments

Work with records older then x days

For the sake of this example, let’s assume that:

  1. we want to work with records older then 60 days
  2. the table name is ‘table’ (how original!)
  3. 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
  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

No Comments