PHP Login Script Tutorial

This PHP login script / tutorial will show you how you can have users register on your site, and log in to access secure areas. I have seen a few tutorials around the web which show how this can be done, but they all seem to lack in security. This user membership tutorial will show a better way of having users authenticated once logged in by using their session ID. Read More…

Posted in MySQL, PHP at October 6th, 2010. 27 Comments.

Simple PHP MySQL Class

That’s right! I have a simple MySQL class file that you can use in your PHP projects. I’ve been using it for years, and it’s never let me down!

You can grab it on my Github: http://github.com/a1phanumeric/PHP-MySQL-Class.

Read More…

Posted in Featured, MySQL, PHP at August 17th, 2010. 7 Comments.

Get Random Row with MySQL Without ORDER BY RAND()

This is an update to a previous post of mine which uses the RAND() method. Using the following code, you can retrieve a random row much, much faster (MySQL 4.1.x/5.0.x), with thanks to Jan Kneschke:

SELECT FROM

AS r1
JOIN (SELECT ROUND(
RAND( ) * (
SELECT MAX( id ) FROM
)
) AS id
) AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 1;

Replace:

  • <COLUMN> with the name of the column(s) you wish to retrieve
  • <TABLE> with the name of the table you wish to retrieve the data from

I've tested this with a table with over 660,000 records, and got a response in 0.0200 seconds, whereas with ORDER BY RAND() i got a response in 2.1599 seconds.

Total Number of Rows:

Cardinality

Old Method:

Old Method

New Method:

New Method

Posted in Featured, MySQL at March 5th, 2008. 14 Comments.

Get Random Row with MySQL

UPDATE: Please see my newer atricle on how to retrieve a random row, faster, without RAND().

This post assumes you know how to create and use a connection to a MySQL database in PHP and have a table named ‘quotes’ as shown below. In this post, I will aim to teach you how to use PHP to pull random quotes from a MYSQL table of quotes. This can be easily extended to pull a random banner as will be explained at the end of the post.

Let’s firstly assume we have a MySQL table similar to the following:

+----+------------------------------------------------+
| id | quote                                          |
+----+------------------------------------------------+
| 1  | I know Karate... and many other Chinese words! |
+----+------------------------------------------------+
| 2  | w00t this is geeky                             |
+----+------------------------------------------------+
| 3  | You're CLINICALLY MENTAL!                      |
+----+------------------------------------------------+

Now, in your PHP code, we need to:

  1. Build a suitable MySQL query to obtain a random result from the ‘quotes’ table.
  2. Store the result of the query to be used in the HTML somewhere.
  3. Output the result in the HTML somewhere.

So, for step one we’d use something like the following:

$sSQLQuery = "SELECT quote FROM quotes ORDER BY RAND() LIMIT 1";
$aResult = mysql_query($sSQLQuery);
$aRow = mysql_fetch_array($aResult, MYSQL_ASSOC);
$sQuoteOfTheDay = $aRow['quote'];

There, the variable ‘$sQuoteOfTheDay’ now has the value of our randomly pulled quote. Let’s just analyse each line of the code above to see what it does.

$sSQLQuery = "SELECT quote FROM quotes ORDER BY RAND() LIMIT 1";

This line stores the MySQL query we’re going to use against the database. It says, in laymans terms, “Select just one random value of the quote field from the table named quotes”. All this line does though is store the query into the variable ‘$sSQLQuery’.

$aResult = mysql_query($sSQLQuery);

This line runs the MySQL query, storing the result of running the query in the variable ‘$aResult’. It’s important that we store the result of the mysql_query in a variable, as the result of running a successful MySQL query using PHP’s function ‘mysql_query’ doesn’t return a nicely formatted array that we can necessarily use.

$aRow = mysql_fetch_assoc($aResult);

This is probably the hardest line for me to explain. Firstly, many of you may have seen a similar line like this used in a ‘while’ loop. However, our MySQL query used the ‘LIMIT 1′ string, so we know we’re only going to get ONE result, hence no need for a loop. The function ‘mysql_fetch_assoc’ takes one parameter: the result of the successful ‘mysql_query’ which as we know is ‘$aResult’. My biggest tip here is to use the ‘assoc’ method wherever possible, as it creates the array in such a way that we can reference each element by the column name, not a number. This is particularly useful if you ever update the MySQL table to have more columns.

Anyway, this line basically says ‘Fill the variable ‘$aRow’ with the CURRENT row of the returned query’. We know that the CURRENT row of the returned query is the ONLY row, hence (again) the lack of a loop. As the result of our query would return something like:

+-----------------------------------------------+
| quote                                         |
+-----------------------------------------------+
| You're CLINICALLY MENTAL!                     |
+-----------------------------------------------+

Our variable (or array) would literally look something like:

Array ( "quote" = "You're CLINICALLY MENTAL!" )

Which leads us on to our last line:

$sQuoteOfTheDay = $aRow['quote'];

Which just assigns the value of ‘$aRow['quote']‘ to the variable ‘$sQuoteOfTheDay’. In other words, ‘$sQuoteOfTheDay’ now has the value of a random quote pulled from the database of quotes.

To use this in an HTML page, we would simply just use this (AFTER the above code has grabbed the random quote from the DB for us):

echo $sQuoteOfTheDay;

Which will output the quote of the day somewhere in the HTML code.

As I said at the beginning, this can be extended easily to pull an image for a banner by simply changing the quotes table to store image paths such as ‘images/my_image.png’ which can then be pulled in the same way, and then output similar to the following:

My Image

Obviously we changed the variable name here to $sImageOfTheDay just to keep things constant.

Hope this has helped someone!

Posted in MySQL, PHP at February 9th, 2008. 3 Comments.