Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP If statement always returning else condition 1

Status
Not open for further replies.

kitackers

MIS
Apr 16, 2004
33
GB
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

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!
 
Hi

Could you specify what are you using to access the database ?
[ul]
[li]mysqli has num_rows, but has no fetch().[/li]
[li]PDO has fetch(), but has no num_rows.[/li]
[/ul]

Feherke.
feherke.github.io
 
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?

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="[URL unfurl="true"]https://fonts.googleapis.com/css?family=Play"[/URL] 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
 
Hi

I would suggest to lower the error reporting level on your development machine. You should receive a notice for that code :
PHP output said:
PHP Notice: Undefined property: PDOStatement::$num_rows in /var/ on line 53

As mentioned earlier, PDO's [tt]PDOStatement[/tt] class has no num_rows property.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top