MySQL Logo

Getting A Random Row From A MySQL Table

26th December 2007 - 1 minute read time

Getting a random row from a MySQL table requires the use of the RAND() function in the ORDER BY clause of the SELECT statement. This will generate a new random number for each row and order them by that new number. In order to get a single row this is combined with the LIMIT clause to limit the result to a single row.

SELECT * FROM theTable ORDER BY RAND() LIMIT 1;
PHP Logo

Odd and Even Numbers in PHP

25th December 2007 - 2 minutes read time

To find if a number is odd or even you can use one of two operators.

The modulo operator (% in PHP) can be used to calculate the remainder of the value divided by 2. This gives a value of 0 for even numbers and a value of 1 for odd numbers. This can be used in an if statement as 0 will equate to false and 1 will equate to true.

#! code Logo

JavaScript Function isNaN

24th December 2007 - 1 minute read time
Small JavaScript function to check if a variable is a number.
PHP Logo

Rounding And Displaying Numbers In PHP

24th December 2007 - 3 minutes read time

To round a number in PHP you can use one of three functions, these are round(), ceil() and floor(). All of these functions take number as the input and will round the value depending on the function used.

To round to the closest integer use the round() function.

round(4.4);  // returns 4

To round down to the nearest whole number use the floor() function.

floor(4.4);  // returns 4

To round up to the nearest whole number use the ceil() function.

PHP Logo

Does A String In PHP Contain A Number?

24th December 2007 - 2 minutes read time

The is_numeric() function in PHP can be used to see if a number contained in a string is numeric. The function returns true is the variable can be parsed into a string, otherwise it returns false. Here are some examples:

PHP Logo

Printing Arrays Using print_r()

20th December 2007 - 2 minutes read time

When debugging PHP code the print_r can be useful if you want to know what an array or object contains. It will take any variable as input and will print off as much information as it can about that variable. The following code.

HTML Logo

Linking An RSS Or Atom Feed to a HTML Document

20th December 2007 - 1 minute read time

Adding a hyper link for an RSS or Atom feed on your web page works, but that's not all you can do. By adding a link to the head section of the page you can allow your users an alternative method of picking up your feed.

To add an RSS feed use this.

<link rel="alternate" type="application/rss+xml" href="http://www.example.com/rssfeed.xml" />

To add an Atom feed use this.