在PHP代码中不接受mysql数据库中的相同用户名

I wrote a code in php. In a form I don't want accepting usernames that exist in username column in a table in mysql. But it doesn't work and now I have same usernames in table. What should I do? this is the code:

 $sql="select * from $tbl_name where username=$username";
 $result=  mysql_query($db_link, $sql);
 $success=FALSE;
    if(mysql_num_rows($result) == 0 ){
        $sql="insert into $tbl_name(username,password) values('$username','$pwd1')";
        mysql_query($sql, $db_link);
        $success=TRUE;
    }

The table name is stored in $tbl_name. The connection to the database is stored in $db_link.

$sql='select COUNT(*) AS cnt from'.$tbl_name.'where username=.'$username;
 $result=  mysql_query($db_link, $sql);
 $res = mysql_fetch_array($result);
 $success=$res['cnt'];
    if($success == 0 ){
       // Do your insert here

    }