I am trying to execute a simple program in PHP with Mysql using IF ELSE Condition
My code is executing IF part but not Else
Please Help me
and here is my code
<?PHP
$userInputEntities = htmlentities($userInput);
echo $userInputEntities;
$username = "admin";
$password = "1234565";
$database = "tele";
$server = "localhost";
$db = new PDO (
"mysql:host=$server;dbname=$database",
"$username",
"$password");
$id = $_REQUEST['cid'];
if ($db) {
$SQL = $db->prepare("SELECT * FROM user WHERE uid = :id");
$SQL -> execute(array(':id'=>$id));
while ($row = $SQL->fetch(PDO::FETCH_ASSOC)) {
if ($id=="$row[uid]") {
echo "Welcome";
} else {
echo "Sorry You Have not registered with our service";
}
}
}
&
My Database table is ‘user’
uid name
1 Manoj
2 pranay only two rows i have in table
If user input availble in table then it should print if part and if user not found then it should print else part
Please Help me, Thankyou
Here If part is printing when IF condition satisfies but not printing else part when If condition fails
The whole
whileblock won’t be executed if your query returns empty set (in case when there’s no user in DB with the givenid). So placingelsein this block is meaningless: ifwhilebody is entered,$idwill ALWAYS be equal to$row['uid']– it’s the condition set inWHEREclause, remember.You need to check the row count instead, and branch your code based on that, replacing the whole
whileblock with something like this:The alternative is changing the query (
SELECT COUNT(*) ...) so it will return the count instead of the whole row, and checking its value against 0.