...

View Full Version : mysqli - Unknown column 'admin' in 'where clause'



surreal5335
07-27-2011, 09:19 AM
I am getting a weird error saying that I am trying to look under a column 'admin'. I have built this function in the past without a problem, but this time I get this error. Here is my code:



function search_user($type, $user_pass)
{
$Database = new mysqli(#############);

mysqli_report(MYSQLI_REPORT_ERROR);

$view_type = 'view_'.$type;

//query database
if($stmt = $Database->query("SELECT ".$view_type." FROM views WHERE view_pass=".$user_pass.""))
{
$show = $stmt->fetch_object();
return $show->$view_type;
$stmt->close();
}
else
{
echo 'Error: could not prepare SQL statement.<br />';
}
}


$user_pass = 'admin'
$view_type = 'view_priv' (another column, if that wasnt too obvious )
view_pass is the column I am trying to search under. Any idea what is going on here?

Thanks a lot

Fumigator
07-27-2011, 05:25 PM
If you were to echo your query, you'd probably immediately see the problem.


$query = "SELECT ".$view_type." FROM views WHERE view_pass=".$user_pass."";
if($stmt = $Database->query($query))
{
$show = $stmt->fetch_object();
return $show->$view_type;
$stmt->close();
}
else
{
echo 'Error: could not prepare SQL statement.<br />Query: $query<br />Error: '.$Database->error;
}


You are not enclosing your string "admin" in single quotes in your query, so it's coming out as WHERE view_pass=admin and MySQL is regarding admin as a column name, which it doesn't find in your table, thus your error. Your query should be coded with single quotes: WHERE view_pass='admin'



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum