PHP Why Else statment strangely doesnt work

<?php else: ?>
php endif
php if statement
php ternary
php if statement multiple conditions
php foreach
php shorthand if without else
php if/else shorthand

I have this following piece of code

include_once("config/connection.php");
$sql = "SELECT * FROM qqqq WHERE LOWER(pdf_ad) LIKE '%" . $aranacak_metin . "%'";
$result = $DBcon->query($sql);

if ($result && is_array($result) && count($result) > 0) {
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

        $yeniad = seo($row['pdf_ad']);
        echo '<form action="/indir/' . $yeniad . '/' . $row['pdf_liste_no'] . '" id="sorgu" method="post">';
        echo '<li class="list-group-item">';
        echo '<input type="hidden" name="id" value="' . $row['pdf_liste_no'] . '">';
        echo '<input type="hidden" name="name" value="' . $row['pdf_ad'] . '">';
        echo '<img alt="' . $row['pdf_ad'] . '" src="' . $row['pdf_resim'] . '" width=120" height="150"><a style="font-size:20px; text-decoration:none; color:black;" href="#"> ' . $row['pdf_ad'] . '</a>  ';
        echo '<button type="submit" class="btn btn-success">';
        echo '<i class="fa fa-arrow-circle-right fa-lg"></i>';
        echo '</button>';
        echo '</li>';
        echo '</form>';
    }
} else {
    echo $aranacak_metin;
}

if statement does its job if the result is set shows the rows but if there is no match in DB else statement doesnt work am i missing something need your help thank you

update:it turns else everytime now

in current scenario $result is always set. so just check you got any match record or not, based on that put conditions as below.

if($result && $result->num_rows && $result->num_rows > 0){

} else {

}

if its PDO query then

if($result && is_array($result) && count($result) > 0){

} else {

}

Alternative syntax for control structures - Manual, the reason why it doesn't work with echo, it's because echo does not return anything, else: echo "Only 'bar' is set."; endif; ?> Weird enough, if you go back to the first the semicolon immediately before the elseif statement, it will also work: Change PHP Script for mysqli / PHP 7.2 to work; What Is The Difference Between A Php Statement And A Php Expression ? PHP - Column count doesn't match value count at row 1; Page doesn't get updated - ajax/php; this line doesn't work in the php part with using ( ' ) instead of ( " ) Convert DataTables to Server Side processing

Try below code:

if ( is_array($result) && count($result) > 0 ) {

   // staff

} else {

   echo $aranacak_metin;
}

PHP & MySQL: Novice to Ninja: Get Up to Speed With PHP the Easy Way, You'll actually see some strange errors appearing inside the <textarea>. it references a variable called $joke which, because of the new if statement, is only One solution would be to load the addjoke.html.php file if there's no ID set and Unfortunately, this doesn't solve the initial problem: to add a new field to the form� I am using a simple if/elseif/else bit of code to generate a header image for my page, I can't seem to understand why it isn't working properly. It determines the category of the page being displayed and displays a header accordingly. Here's the code in my header.php:

Changed my php version it worked ?

Expert PHP and MySQL: Application Design and Development, I have a single method, DbAccess: :update, to do the work for me. Unlike DbAccess: :query, it doesn't take an SQL statement but rather a DbAccess: : update might seem like a strange method, but it exactly reflects howl $f=$v"; } $ upd = substr($upd, 2); if (emp'EY($data[$pkfield])) $sql = "insert $table set $upd"; else� Nested if else in PHP The if-else-if-else statement lets you chain together multiple if-else statements, thus allowing the programmer to define actions for more than just two possible outcomes. Check given character is vowel or consonant.

The List of the 10 Most Common Mistakes That PHP Developers , Using references in foreach loops can be useful if you want to operate on As a result, when we go through the second foreach loop, “weird stuff” appears to happen. of the $values array, and THIS copy doesn't // contain a 'test' element ( which of memory” condition in libmysqlclient when using PHP's mysql extension. The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE. Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example.

Three Tips for Writing Airtight PHP Conditions • WPShout, In PHP, conditions are the core of conditional statements such as like if - statements and if else statements, in our introduction to PHP control flow. It's kind of like an older car that doesn't ding when you don't wear your seatbelt. That gives The code above will run and print out “Thanks for the number. You can use conditional statements in your code to do this. In PHP we have the following conditional statements: if statement - executes some code if one condition is true; ifelse statement - executes some code if a condition is true and another code if that condition is false

Shorthand comparisons in PHP, The ternary operator is a shorthand for the if {} else {} structure. If this $ condition evaluates to true , the lefthand operand will be assigned to $result . The following, even though it seems logical; doesn't work in PHP: the ternary operator in PHP is left-associative, and thus parsed in a very strange way. You can't use the header function to redirect a page after any type of output. When you execute your debug 'echo' statement prior to your 'header' function, the header will not work. This may be impacting whether things appear to work or not. I have never had to append a semicolon to the end of MySQL statements.

Comments
  • because $result is always set.
  • You have to check if $result is empty or not (!empty($result)), $result is always set it returns true or false every time,
  • i did what you guys say but still i dont get any else message
  • @EnginYücelen Can you please post the updated code? With the changes given by Devsi and Robin
  • Your code is vulnerable to SQL injection. You should use prepared statements.
  • i'm getting error Notice: Undefined property: PDOStatement::$num_rows
  • @EnginYücelen Then change it to ($result && isset($result->num_rows) && $result->num_rows > 0) .
  • @EnginYücelen use second one in my answer if u r using PDO.
  • Warning: count(): Parameter must be an array or an object that implements Countable i'm having this error but still it doesnt give me the else
  • what you have got in $result ?