Information Technology Reference
In-Depth Information
instant search results, the feature we discussed earlier. We'll start by creating the
database, using the schema in Listing 12-6. This listing begins by creating a table, and
then populating it with several test usernames (“hardcore,”“dinosaur,” etc.).
Listing 12-6. the SQL Schema for the Users Table
CREATE TABLE IF NOT EXISTS `users` (
`name` varchar(25) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`name`) VALUES
('hardcore'),
('dinosaur'),
('yelius'),
('tep');
Use the preceding schema to create the users table in your MySQL database. (MySQL
is available at http://www.mysql.com/products/community/ .) N ote that you can name the
database whatever you wish, but in this example it's called “datab.” Now we need a
simple PHP script which takes a value and checks it in the database. Listing 12-7 shall
do nicely.
Listing 12-7. The check_name.php Script
<?php
$name = $_GET['u'];
$user = "root";
$pass = "";
$server = "localhost";
$db = "datab";
mysql_connect($server,$user,$pass) or die("Can not connect");
mysql_select_db($db) or die("No such database");
$query = "SELECT * FROM `users` where `name` = '$name'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0)
{ echo "<font color=green>Available!</font>"; } else { echo "<font color=red>Not
Available :( </font>"; }
?>
The preceding script is fairly simple. It connects to the database and does a simple
query to see in how many rows the name is entered. If the name is not found on any
rows, then it's available; the script sends back “Available” (and there is presumably
much rejoicing). However, if it's not available, because the database already has a row
with the name in it, the script returns the considerably sadder “Not Available.” You can
test this by visiting the check_name.php script in your web browser, including
&u=test with some sort of test username. For example, in my testing environment,
http://localhost/~jon/uname/check_name.php?u=dino returns “Available,” while
http://localhost/~jon/uname/check_name.php?u=dinosaur returns “Not Available.” If
this is working for you, then you're ready to proceed.
 
Search WWH ::




Custom Search