If i’ve database table with users(name,job) (john,Poster) and i made query with MySQLi using this code and that would works.
$job = "Poster";
$statement = $con->prepare("SELECT * FROM users WHERE `job` = ?");
$statement->bind_param("s",$job);
$statement->execute();
$statement->bind_result($name,$job);
while ($statement->fetch()){
echo $name;
}
what if i made $job = "NOTHING"; and there was no results then how can i show error such as echo "No reuslts found"; !! the above code if $job was not found it will show nothing. ~ thanks
EDIT
this one didn’t worked too 🙁
$job = "NOTHING"; // should not found and should gives error
if ($statement = $con->prepare("SELECT * FROM users WHERE `job` = ?")){
$statement->bind_param("s",$job);
$statement->execute();
$statement->bind_result($name,$job);
while ($statement->fetch()){
echo $name;
}
}else{
echo "No results found dude";
}
Do not use mysqli. It is unusable with prepared statements.
Use PDO instead.
With PDO your code would be shorter, sensible and works.
You need to create a connection first. Something like this
You can see details on the manual page
As a further step you can move to some helper library, which will make your code even shorter:
But it would be wise to get yourself familiar with raw API functions first.