×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

PHP If statement always returning else condition

PHP If statement always returning else condition

PHP If statement always returning else condition

(OP)
Oon my Search page I've got dropdowns that are linked to an SQL database and pull a list of names (e.g. the combination accent northern-brown-male-manchester brings up A & B, southern-grey-female-london brings up X & Y)

This works fine. What I'm trying to do is add an if statement so if no rows are returned, I get a default message of
"No actors match your criteria, please try again"

The problem is, I always get the default message, even when records are being returned

CODE --> php

if ($result->num_rows > 0){
                        
                            echo '<table><tr><th>id</th><th>name</th></tr>';

                    while($row=$result->fetch())   {
                       
                    
                    echo '<tr><td>'.$row['id'].'</td><td>'.$row['name'].'</td></tr>';

                                                    }
                    echo '</table>';
                    }
                        
                    else
                    {     
                        echo '<p> No actors match your criteria, please try again </p>';
                    } 


Is it a bracket problem?....It's always a bracket problem!

RE: PHP If statement always returning else condition

Hi

Could you specify what are you using to access the database ?
  • mysqli has num_rows, but has no fetch().
  • PDO has fetch(), but has no num_rows.

Feherke.
feherke.github.io

RE: PHP If statement always returning else condition

(OP)
So, this is the full code (with the sensitive stuff removed), I'm using PDO, so I guess this isn't the right route if it does't have num_rows, should I be using something like row count?

CODE --> php

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

	<title>Home</title>
<link rel="styleSheet" href="mystyles2.css" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Play" rel="stylesheet"> 


<div id="navcontainer">
<ul id="navlist">
<li><a href="home.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="search.php">Search</a></li>
<li id="active"><a href="processOLD2.php" id="current">Result</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
    

 <?php
    
                    // set up variables
                    $username='XXXX';
                    $password='XXXX';
                    $hostname='XXXX';
                    $dbname='XXXX';
                    $txt_accent = $_POST['txt_accent'];
                    $txt_hair = $_POST['txt_hair'];
                    $txt_gender = $_POST['txt_gender'];
                    $txt_location = $_POST['txt_location'];
                    
    
                    // connect to database
                    $dbh= new PDO('mysql:host='.$hostname.';dbname='.$dbname,$username,$password);
        
                    // generate SQL statement
       
                    $result=$dbh->prepare("SELECT * FROM performers WHERE accent='$txt_accent' AND hair='$txt_hair' AND gender='$txt_gender' AND location='$txt_location'");
				
                    // execute SQL query
                    $result->execute();
        
                    // display results
                            
    
                    if ($result->num_rows > 0){
                        
                            echo '<table><tr><th>id</th><th>name</th></tr>';

                    while($row=$result->fetch())   {
                       
                    
                    echo '<tr><td>'.$row['id'].'</td><td>'.$row['name'].'</td></tr>';

                                                    }
                    echo '</table>';
                    }
                        
                    else
                    {     
                        echo '<p> No actors match your criteria, please try again </p>';
                    }
                

                    // close database connection
                    $dbh = null;
            ?>
    
    
    <!-- this piece of code successfully pulled all of the names from the data base as a drop down
                   
                    while($row=$result->fetch())
                    {
                        $name = $row['name'];
                        echo "<option value='$name'>$name</option>";
                    }
    -->

</html> 

Thanks for any help you can give me

RE: PHP If statement always returning else condition

Hi

I would suggest to lower the error reporting level on your development machine. You should receive a notice for that code :

Quote (PHP output)

PHP Notice: Undefined property: PDOStatement::$num_rows in /var/www/kitackers.php on line 53

As mentioned earlier, PDO's PDOStatement class has no num_rows property.

Feherke.
feherke.github.io

RE: PHP If statement always returning else condition

(OP)
Ok - Thanks Feherke, appreciate the help

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close