Jump to content

MySQLi Query with PHP Variable - Error?


Mko

Recommended Posts

Hey all,

I have the following code:

	
$searched = mysqli_real_escape_string($database, $_GET['searched']);
echo $_GET['searched'].'<br />';
echo $searched.'<br />';
$personal_un_s = (string) $searched;
$query = "SELECT members.id, members.name FROM members WHERE members.name = ". $personal_un_s ."";
$userid_query = mysqli_query($database, $query) or print(mysqli_error($database));
if (mysqli_num_rows($userid_query) < 1) {
	echo 'Error!';
} else {
	$userid_array = mysqli_fetch_array($userid_query);
	$userid = $userid_array[0];
}

 

However, when I run this, I get this error:

Unknown column 'Mko' in 'where clause'

 

The $_GET['searched'] functions correctly, however any value does not seem to be 'valid' for the MySQLi Query.

I'm pretty sure it's some very small problem I have...though, can anyone help me in identifying the issue?

 

 

Thanks,

Mark

Link to comment
Share on other sites

Strings must be surrounded in quotes. Right now you're trying to compare two columns, not a column to a string.

You also can fix your quotes.

$query = "SELECT members.id, members.name FROM members WHERE members.name = '$personal_un_s'";

Link to comment
Share on other sites

As jesirose points out, when you use double quotes for a string in PHP:

 

$some_var = "Some string";

 

PHP is checking the double quotes for variables (it's parsing the string as PHP). Using single quotes will not parse a string to PHP. So if you're going to concatenate a variable to a string, use single quotes for the string, otherwise dont' concatenate:

 

$string = 'some string.';
$other_string = 'this is '.$string;
echo $other_string; // Echoes: this is some string

$other_string = 'this is $string';
echo $other_string; // Echoes: this is $string

$other_string = "this is $string";
echo $other_string; // Echoes: this is some string

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.