<?php
function searchDatabase($key, $value)
{
$key = $key;
$query = "SELECT * FROM user_data WHERE username='$value'";
$result = mysqli_query(loadDatabase(), $query);
$numRows = mysqli_num_rows($result);
if ($numRows > 0)
{
return true;
}
else
{
return false;
}
}
?>
So I am using this code to search through my database to reveal a match on a key/value pair, but $key
doesn't find the correct column in my database when passed into the query function. If I replace it with the word username, it matches fine. Is it a type issue? I am not explicitly stating its type so I can search other columns with the same function.
username: varchar(40)
<?php
function searchDatabase($key, $value)
{
$query = "SELECT * FROM user_data WHERE ".$key." = '".$value."'";
$result = mysqli_query(loadDatabase(), $query);
$numRows = mysqli_num_rows($result);
return ($numRows > 0);
}
?>
Try this
$query = "SELECT * FROM user_data WHERE ".$key."='$value'";
Like so:
function searchDatabase($key, $value)
{
$query = "SELECT * FROM user_data WHERE $key='$value'";
$result = mysqli_query(loadDatabase(), $query);
$numRows = mysqli_num_rows($result);
return ($numRows > 0);
}
The problem is in the query. PHP variables are not properly echoed in the query.
<?php
function searchDatabase($key, $value)
{
$query = "SELECT * FROM user_data WHERE username='".$value."'";
$result = mysqli_query(loadDatabase(), $query);
$numRows = mysqli_num_rows($result);
return ($numRows > 0);
}
?>