Archive for category MySQL

Backup and Restore a web site in CPANEL

It’s a good practice to make backups prior to submit any changes to your site.

Time and again people skip the backup to find themselves with problems and not having an easy and quick way to undo their changes. This may not seem a big problem, but if the changes were done to the web site of your business and now the web site is not working probably, this means you’re losing money!

Backing up a site is easy and even more if your web server uses CPANEL.

Consider the following steps to BACKUP your site:

  • To Backup the files of your site:
    1. On CPANEL select option Backups
    2. Under Home Directory select option Download a home directory Backup
    3. Select Save File
  • To Backup the Databases of your site:
    1. On CPANEL select option Backups
    2. Under Download a MySQL Database Backup select the Database you wish to Backup
    3. Select Save File
    4. Repeat Steps 2 and 3 for any other Database you wish to backup

Consider the following steps to RESTORE your site:

  • To Restore the files of your site:
    1. On CPANEL select option Backups
    2. Under Restore a Home Directory Backup select Browse and locate the backup you want to restore from
    3. Select Upload
  • To Restore the Databases of your site:
    1. On CPANEL select option Backups
    2. Under Restore a MySQL Database select Browse and locate the backup you want to restore from.
    3. Select Upload
    4. Repeat Steps 2 and 3 for any other Database you wish to restore
  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

No Comments

How to select the First row from a MySQL table

You can use the following command to get the first row from a MySQL table:

SELECT * FROM table LIMIT 1

Explanation:

table - table name

By limiting the returns to 1 (LIMIT 1) we get the first row from the table.

Example:

SELECT * FROM tusers LIMIT 1

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

2 Comments

How to select the Last row from a MySQL table

You can use the following command to get the last row from a MySQL table:

SELECT * FROM table ORDER BY field DESC LIMIT 1

Explanation:

table - table name

field - table field

This command orders the table by DESCENDING order and limits the number of rows returned to 1. Since DESCENDING reverses the normal table order, we get its last row.

Example:

SELECT * FROM tusers ORDER BY user_id DESC LIMIT 1

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

No Comments

Select random rows from a MySQL table

If you ever come across the need of randomly select rows from a MySQL table:

SELECT * FROM table WHERE field1 = value ORDER BY RAND() LIMIT x

Explanation:

table - table name

field1 - table field

value - value you want field1 to be

x – number of rows to return

Example:

SELECT * FROM tusers WHERE active_user = ‘Yes‘ ORDER BY RAND() LIMIT 10

This command returns 10 random rows of active users from table tusers.

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

No Comments

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

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