PHP Logo

Update Or Insert A Row With MySQL And PHP

6th April 2008 - 2 minutes read time

Many situations arise where you need to either insert or update some data in a table but which you will not be certain as to which function to perform. A common solution is to do a query on the table first to see if the data exists and then insert if it doesn't and update if it does. However, this creates an unnecessary overhead in that every time the code is run at least 2 queries are run.

A better way is to try to update the table and then use the mysql_info() function to detect how many rows where updated in the query and how many rows matched the parameters in the update query.

Take the following query.

MySQL Logo

Random Number Range In MySQL

5th April 2008 - 1 minute 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.

PHP Logo

Latest MySQL Errors With PHP

4th April 2008 - 2 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.

PHP Logo

HTML Checkbox To PHP Array

3rd April 2008 - 2 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.

PHP Logo

Creating A 404 Page In PHP

2nd April 2008 - 2 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 - 7 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 - 2 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.

PHP Logo

Creating A URI Slug With PHP

28th March 2008 - 2 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 - 3 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.

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

Here is a quick explanation of each code.