Question

I am new to PHP and am unsure as to the syntax of mysql in php. I am trying to check a username from a data_table with the help of the following SELECT statement but seem to be encountering a syntax error. I would really appreciate any help in this matter.

$myquery = sprintf("SELECT `Username` FROM `dataTable` WHERE `Username` = \\%s", mysqli->real_escape_string($loginID));

 $userNameCheck = mysqli->query($myquery); 

 if($userNameCheck)
 {
    echo "query succeeded";
 }
Was it helpful?

Solution 2

saw that you are missing the $ sign at mysqli, don't know if that is a typo here or in your code.

$myquery = "SELECT `Username` FROM `dataTable` WHERE `Username` = '".$mysqli->real_escape_string($loginID)."'";
$userNameCheck = $mysqli->query($myquery);

And then test what @Ray wrote

OTHER TIPS

Skip the sprintf.

  $myquery = "SELECT `Username` FROM `dataTable` WHERE `Username` = '".mysqli->real_escape_string($loginID)."'";

You still need quotes, even with your... unusual method of query building. Also, I'm not sure what the double-backslash is in aid of.

$myquery = sprintf("..... `Username` = '%s'",mysqli...);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top