Reputation: 73
I'm trying to set up a if condition whereby if $flag < 1 an apologize.php will be rendered. However it seems that the condition will be triggered even when $flag>1.Does anyone know the problem?
This is my php code.
$flag = query("SELECT flag FROM friend WHERE id = ? AND username = ? ",$_SESSION["id"],$username[0]["username"]);
if($flag[0]["flag"] < 1 ||$flag = false);
{
apologize("Sorry! He/she is not your friend.");
}
This is my code for the function query()
function query(/* $sql [, ... ] */)
{
// SQL statement
$sql = func_get_arg(0);
// parameters, if any
$parameters = array_slice(func_get_args(), 1);
// try to connect to database
static $handle;
if (!isset($handle))
{
try
{
// connect to database
$handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD);
// ensure that PDO::prepare returns false when passed invalid SQL
$handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (Exception $e)
{
// trigger (big, orange) error
trigger_error($e->getMessage(), E_USER_ERROR);
exit;
}
}
// prepare SQL statement
$statement = $handle->prepare($sql);
if ($statement === false)
{
// trigger (big, orange) error
trigger_error($handle->errorInfo()[2], E_USER_ERROR);
exit;
}
// execute SQL statement
$results = $statement->execute($parameters);
// return result set's rows, if any
if ($results !== false)
{
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
else
{
return false;
}
}
Upvotes: 1
Views: 184
Reputation: 336
You cannot use =
as operator, because it return true
if it can set the variable to false
.
Use ==
.
Upvotes: 0
Reputation: 324620
$flag = false
should be $flag == false
, !$flag
or $flag === false
, and ideally should be before you attempt to access it as an array:
if( !$flag || $flag[0]['flag'] < 1)
Also you have an extra ;
- this is actually the exact reason why I obstinately refuse to put a newline before {
.
Upvotes: 0
Reputation: 4060
Your problem is two fold
First off you have a ;
at the end of your if statement.
Second off you are setting $flag = false
instead of comparing it $flag == false
. You probably want to compare it using the ==
equality operator.
Upvotes: 2