Jump to content

Checking a unique username


peuge

Recommended Posts

I am using a function which checks if a username is unique in a database. I have the following:

 

function check($table, $field, $compar_to)
{
  $sql = "SELECT  '.mysql_real_esacape_string($field).'
          FROM '.mysql_real_escape_string($table).'
          WHERE "'.mysql_real_escape_string($field).'" = "'.mysql_real_escape_string($compar_to).'"";

  $query = mysql_query($sql);

if(mysql_num_rows($query)==0)
{
	return TRUE;
}
else 
       {
	return FALSE;
}
}

 

The error code I get is this:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in D:\Programs\EasyPHP 2.0b1\www\DataBase\functions.php on line 12

 

and I call the function via this:

checkUnique($tbl_name,'username', $_POST['myusername'])

 

The above function is in an if loop and the condition is if this is true then do X.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/98856-checking-a-unique-username/
Share on other sites

You can't start a string with a double quote then end with a single quote. You query should be:

 

<?php
  $sql = "SELECT  `".mysql_real_esacape_string($field)."`".
          " FROM `".mysql_real_escape_string($table)."`".
          " WHERE `".mysql_real_escape_string($field)."` = '".mysql_real_escape_string($compar_to)."'";
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.