MattClaff MattClaff - 1 year ago 82
MySQL Question

Check if username exists in a database

pretty stuck on trying to prevent the user from registering if the username exists. Here is my code:

//get all the names and values that have been posted.
$username = $_POST['username'];
//check if username exists
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$query);
echo"name already exists";
{ // excecute insert query

I have tried loads of other stuff to get it working but for some reason my code doesn't like me......

Answer Source

change mysql_num_rows to mysqli_num_rows

if(mysqli_num_rows($result)>=1)//You are mixing the mysql and mysqli, change this line of code
            echo"name already exists";
            {  //

use bind param or proplerly escape your value, before using it in query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download