Archive for category MySQL
Backup and Restore a web site in CPANEL
Posted by Rui Miguel Feio in MySQL, PHP, WordPress on August 16, 2009
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:
- On CPANEL select option Backups
- Under Home Directory select option Download a home directory Backup
- Select Save File
- To Backup the Databases of your site:
- On CPANEL select option Backups
- Under Download a MySQL Database Backup select the Database you wish to Backup
- Select Save File
- 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:
- On CPANEL select option Backups
- Under Restore a Home Directory Backup select Browse and locate the backup you want to restore from
- Select Upload
- To Restore the Databases of your site:
- On CPANEL select option Backups
- Under Restore a MySQL Database select Browse and locate the backup you want to restore from.
- Select Upload
- Repeat Steps 2 and 3 for any other Database you wish to restore
How to select the First row from a MySQL table
Posted by Rui Miguel Feio in MySQL on July 6, 2009
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
How to select the Last row from a MySQL table
Posted by Rui Miguel Feio in MySQL on July 6, 2009
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
Select random rows from a MySQL table
Posted by Rui Miguel Feio in MySQL on July 6, 2009
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.
How to identify a MySQL query problem
Posted by Rui Miguel Feio in MySQL, PHP on July 1, 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).
Work with records older then x days
Posted by Rui Miguel Feio in MySQL on June 29, 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
Follow Me!