O'Reilly logo

Learning PHP, MySQL, and JavaScript by Robin Nixon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

rncheckuser.php

To go with rnsignup.php, here’s Example 20-7, rncheckuser.php, the program that looks up a username in the database and returns a string indicating whether it has already been taken. Because it relies on the functions sanitizeString and queryMysql, the program first includes the file rnfunctions.php.

Then, if the $_POST variable 'user' has a value, the function looks it up in the database and, depending on whether it exists as a username, outputs either “Sorry, already taken” or “Username available.” Just checking the function mysql_num_rows against the result is sufficient for this, as it will return 0 for not found, or 1 if it is found.

The HTML entity ← is also used to preface the string with a little left-pointing arrow.

Example 20-7. rncheckuser.php
<?php // rncheckuser.php
include_once 'rnfunctions.php';

if (isset($_POST['user']))
{
    $user = sanitizeString($_POST['user']);
    $query = "SELECT * FROM rnmembers WHERE user='$user'";

    if (mysql_num_rows(queryMysql($query)))
        echo "<font color=red>&nbsp;&larr;
             Sorry, already taken</font>";
    else echo "<font color=green>&nbsp;&larr;
             Username available</font>";
}
?>

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required