MySQL Logo

Random Number Range In MySQL

5th April 2008 - 2 minutes read time

To create a number between one value and the next you can use the following formula, where i is the lower end of the range and j is the higher end of the range.

FLOOR(i + RAND() * (j – i))

Rather than put in (j-i) in your query you should put in the result. So for a number between 1 and 10 you would make i = 1 and j = 11. 11-1 = 10 so the query would run like this.

SELECT FLOOR(1 + (RAND() * 10));

For a number between 64 and 104 you would use the following query.

SELECT FLOOR(64 + (RAND() * 41));
PHP Logo

Latest MySQL Errors With PHP

4th April 2008 - 3 minutes read time

Pinpointing where an error in a program is occurring is not always easy. This can be even more of a problem when you write an SQL query that produces an error. Take the following select statement, which is clearly wrong.


ELECT * FROM table;

I tend to do this at least every now and then, and don't usually spot it until it's too late. So when I then run mysql_query() on this statement nothing happens. As far as I can tell form the code the table contains no data.

To get the error message that is produced from the SQL you can use the mysql_error() function to return the error message. The single optional parameter is the resource identifier for the MySQL connection, but if you leave this out then PHP will use the latest resource created.


echo mysql_error();

This will print the following line in your PHP output.

PHP Logo

HTML Checkbox To PHP Array

3rd April 2008 - 3 minutes read time

To create a simple HTML check box use the following bit of code.


<input type="checkbox" name="option2" value="Milk" />

To set the checkbox as filled in include a checked attribute. To make the control XHTML compliant you will need to do the following.


<input type="checkbox" name="option2" value="Milk" checked="checked" />

When the form is posted this value is sent to PHP with the $_POST superglobal array.

To link several checkboxes together to make them into an array in the PHP $_POST array you need to make all of the checkboxes have the same name, and each name must end in "[]".

PHP Logo

Creating A 404 Page In PHP

2nd April 2008 - 3 minutes read time

Setting up a 404 page on your site will help users when they navigate to a page that doesn't exist. Rather than dropping them into a scary server message you can give them a nice friendly error page. The first step is to make sure that if the user generates a 404 error they are given a nice page. Add this line to your .htaccess file.


ErrorDocument 404 404.html

Now when a user hits a non existent page they will see you nice error page. However, you sometimes will want to produce a 404 page when the server doesn't give out a 404 error, for example, if you have the following URL.

PHP Logo

Array Sorting Algorithms In PHP

1st April 2008 - 8 minutes read time

There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations, especially when sorting things like dates as PHP usually guesses which value is higher than the other and can produce odd results. However, there are plenty of sorting algorithms available than can allow you to sort an array in any way you want.

The simplest of these is called the bubble sort. Here is a function that will sort an array of values using the bubble sort algorithm.

PHP Logo

PHP array_merge() Function Improvement

31st March 2008 - 3 minutes read time

The array_merge() function in PHP is a handy way of adding one or more arrays together. Here is an example of how to use it.

$array1 = array(3, 21, 12); // set up first array
$array2 = array(63, 1, 9); // set up second array
$array3 = array_merge($array1, $array2); // merge arrays
print_r($array3); // print!

This will print the following.

Array
(
 [0] => 3
 [1] => 21
 [2] => 12
 [3] => 63
 [4] => 1
 [5] => 9
)

The only problem with this function is that it resets any numeric keys, so the following example would produce the wrong result.

PHP Logo

Creating A URI Slug With PHP

28th March 2008 - 3 minutes read time

The use of mod_rewrite on a site can have a powerful effect on search engine positioning, but to do it properly you will need to create a "slug" for each page. A slug is a lowercase alphanumeric version of the page title, with any spaces removed.

To get a slug you will need to use a function to turn a readable page title into a string that can be used as part of a URI.

This function is taken from Bramus and his excellent article about creating a post slug, and it does the job very nicely.

PHP Logo

PHP Browser Detection

27th March 2008 - 4 minutes read time

Retrieving the current user agent using PHP is done via the use of the $_SESSION super global array. The following line of code will print off your user agent.

echo $_SERVER['HTTP_USER_AGENT'];

For Firefox on Windows this user agent will look like this.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12

This is all fine, but what about getting more meaningful information, like just the version number. Here is a function that will get the version number (major or minor) from anyone visiting a page with Internet Explorer.

PHP Logo

PHP Easter Eggs

26th March 2008 - 2 minutes read time

There are a couple of hidden features that you can see on just about any PHP driven website. You can get either an image or a list of credits for PHP by appending one of the following strings to the end of the URL.


?=PHPE9568F34-D428-11d2-A769-00AA001ACF42
?=PHPE9568F35-D428-11d2-A769-00AA001ACF42
?=PHPE9568F36-D428-11d2-A769-00AA001ACF42
?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000

Here is a quick explanation of each code.

PHP Logo

The glob() Function In PHP

25th March 2008 - 5 minutes read time

The glob() function in PHP uses simple pattern matching to find files and directories in a directory and return those file names as an array. It simplifies all of the PHP directory functions, so instead of opening the directory and then reading every file name one by one, you can just use glob() and so it in one function call. Additionally, glob() works very closely with the file system and so it very quick.

Here is a simple example.


$globOut = glob('*');

The $globOut variable now contains a list of all of the files and directories in the root folder. If you want to find all of the files ending with HTML in a directory then use the following.

The function takes another parameter which is a constant that defines some ways of changing the output. Here is a list of the available constants.