Home » Archive

Articles tagged with: MySQL

MySQL »

[5 Jan 2010 | No Comment | 159 views]

Here’s the quickest way to sort data in MySQL after retrieving the last 10 entries with the LIMIT and ORDER BY DESC commands. If you simply do a ORDER BY DESC LIMIT 10, this will return the last 10 results in reverse order (newest first). Adding an inline SELECT command will sort the results after retrieving the last 10 items. (oldest first)

SQL Command

$sql = "SELECT * FROM (SELECT * FROM [TABLE] WHERE ORDER BY modified_date DESC LIMIT 10) AS tbl ORDER BY tbl.modified_date"

This will retrieve the last 10 rows while sorting by date chronologically. I used this SQL command for …

MySQL »

[17 Jul 2009 | 3 Comments | 847 views]

If your site experiences the infamous “Mysql : Too many connections” error, you will need to tune your MySQL server to handle the load of a heavily used website.

There are some configuration variables that you can adjust to make your site handle more connections.
The wait_timeout default in MySQL is 28840 seconds (8 hours), which can seriously limit the number of open connections you have on your page. I adjusted mine to 60 seconds. This frees up the thread after idle timeout, which allows for more connections to connect. The 8 hour timeframe is fine if you only have a few connections to the server. For popular websites, its best to make this value as low as possible. Set this variable to the maximum time your page typically runs. For instance, if you have a page that needs to retrieve some data processed from another server (60-80 seconds) and you only use one mysql_connect, you may lose the mysql connection for processing the remaining parts of the page if your wait_timeout is too low. If the processing takes much longer, a workaround is to use multiple mysql_connects to prevent an idle connection from disconnecting.

MySQL, PHP »

[11 Jun 2009 | 2 Comments | 344 views]

I’ve been working on tuning my Apache/MySQL/PHP configuration lately to see how to improve the performance. Lately, I’ve been noticing some crawlers that have been ignoring my robots.txt file and hitting my server really hard with several page requests every second. So, I decided to run some tests to see where I could speed up my website in the code.

MySQL, PHP »

[12 Mar 2009 | One Comment | 668 views]

A good practice is to check input strings to make sure users don’t put in mySQL commands in your server. For instance, if a username or password POST variable isn’t filtered, there is a potential for an injection like ‘OR myusername=’. In the past, I’ve been using my own PHP toolkit to “clean” the input variables. But recently, I began searching to see if there are a built-in solution in PHP for this, especially since I’m converting a script written in Python that had the filter MySQLdb.escape_string. Enter mysql_real_escape_string()

MySQL, PHP »

[1 Mar 2009 | No Comment | 351 views]

Converting the datetime format is really simple if you let MySQL do the formatting for you.

MySQL 5.1 SELECT DATE_FORMAT manual

For instance, if you wanted to convert the datetime into something that reads
function ConvertNiceDateOnly($date)
{
// Convert mySQL date to nice formatted date
$normaldate = mysql_query("SELECT DATE_FORMAT('$date','%M %e, %Y')");
$normaldate = mysql_fetch_row($normaldate);
$normaldate = $normaldate[0];
return $normaldate;
}

MySQL, PHP »

[26 Jan 2009 | No Comment | 683 views]

$unixseconds = strtotime($mysqldate);

For instance, you can use this to write a timeout script for login failures. Usually, a system should lock after 3-5 consecutive failed login attempts. I save the timestamp after the 5th consecutive login failure, then run a check on this timestamp if the current time is within the ~5-10 minute lockout window. 5 minutes is 300 seconds, 10 minutes is 600 seconds.

MySQL »

[30 Dec 2008 | No Comment | 760 views]

I looked through the documentation on MySQL and it looks like they removed the “rename database” option. Here’s a workaround:

mysqladmin create [Name of new database]
mysqldump –opt [Name of old database] | mysql [Name of new database]

MySQL »

[11 Dec 2008 | No Comment | 368 views]

Here’s a quick reference to reset the MySQL auto_increment field to 1 and delete ALL rows in a table. This is useful in case you had to do some testing and wanted to reset the primary key field back to 1 and clear out the table.

truncate table [NAME OF TABLE]

If you don’t want to wipe out the table, you can reset the auto_increment by using:

alter table [NAME OF TABLE] auto_increment=1