tags:

views:

45

answers:

1

I have created a form. Now I want to check if the username entered by the user is already on the database or not. If yes, I want it to display an error message.

Thanks

A: 

its general question

server side check db is like :

$qry = sprintf( 'SELECT `id` FROM `users` WHERE `userName` = \'%s\'', mysql_real_escape_string( $_POST['userName'] ) );
$res = mysql_query( $sql );
$num = mysql_num_rows( $res );

if ( $num )
{
    echo 'This user already exists';
}  

in the client you can use jquery + ajax call to call this function and show error message.

you can see this article to see what to do in the client :

http://www.ajaxlines.com/ajax/stuff/article/check_username_availability_using_ajax_and_jquery.php

Haim Evgi
*(sidenote)* the [mysql extension](http://de2.php.net/manual/en/book.mysql.php) is no longer maintained (or very little) and you are encouraged to use the [improved mysqli extension](http://de2.php.net/manual/en/book.mysqli.php) instead
Gordon
good point @gordon, I hope this will be his last problem
Haim Evgi
@Gordon, That is a lie, It is still supported in the sense it will get a bug fix if needed. Not to mention considering that PHP and MySQL is open source you cannot say it is not maintained. Also the mysqli extension provides no significant enhancements over the standard mysql extension.
Petah
@Petah Bugfixes classifies as *very little* to me. I do not consider prepared statements insignificant.The PHP Manual explicity states *"Although this MySQL extension is compatible with MySQL 4.1.0 and greater, it doesn't support the extra functionality that these versions provide. For that, use the MySQLi extension."*. In addition, ask Johannes Schlüter if you dont believe me.
Gordon
@Gordon, Prepared statements become obsolete when you use DAO
Petah
@Petah what does that has to do with using mysqli over mysql? I am willing to rephrase from *"no longer actively maintained"* to *"no longer actively developed"* but it doesnt change the fact that the code developers encourage you to use mysqli.
Gordon