I'm using this query to check the username is already exist or not in database using ajax so till now i hv only few records in DB bt in future username list gonna be huge so i want to improve the performance of query in such a way it will take less time to check username existence.
how can i improve following query ? is indexing on db is feasible solution for this ?
check.php
<?php
if(isset($_POST['user_name']))
{
$user_name=$_POST['user_name'];
include("include/conn.php");
$sql_check = mysql_query("select userid from `vector`.`signup` where userid='".$user_name."'")
or die(mysql_error());
if (mysql_num_rows($sql_check)>0)
{
echo "no";
}
else
{
echo "yes";
}
}
?>
my jquery code
<script language="javascript">
$(document).ready(function()
{
$("#uname").blur(function()
{
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
$.post("check.php",{ user_name:$(this).val() } ,function(data)
{
if(data=='no') //if username not avaiable
{
$("#msgbox").fadeTo(200,0.1,function()
{
$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function()
{
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
</script>
Creating an index on the userid column of the vector.signup table will probably solve your performance issues.
As it stands you have a massive security flaw in your script. You should never inject POST data directly into a query, because you open yourself up to a SQL injection attack. You should escape your data first:
$user_name = mysql_real_escape_string($_POST['user_name']);
You can read more here:
http://php.net/manual/en/function.mysql-real-escape-string.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With