PHP With Row Number And If Else

I am running a sql query and getting a count of the results using PHP. The below is the relevant parts of my syntax and what I want to do is if the count returned is >= 1 then display the grid (as it will be populated with 1 or more rows), but if the row count returned is 0 then display a warning on screen notifying the user.

My issue with the code is that it always returns the grid!

<?php

//Connection to server to run sql query and return results

$numofrows = mssql_num_rows($tsql);

?>

if ($numofrows >= 1) {
    //Build out HTML Table
} else {
    //write on screen there are no results to display
}

EDIT
This is how I am setting it up which appears to be the same as the link in the comments

<?php

//Connection to server to run sql query and return results

$numofrows = mssql_num_rows($tsql);

?>

if ($numofrows >= 1) {
    <table border="1">
    <thead>
    <tr>
    <th >Header 1 </th>
    <th>Header 2 </th>
    <th>Header 3 </th>
    <th>Header 4 </th>
    <th>Header 5</th>
    <th>Header 6 </th>
    </tr>
    </thead>
    <tbody>
    <?php
    foreach( $query as $res ) { 
    print "<tr>";
    print "<td>" .$res->Field1."</td>";
    print "<td>" .$res->Field2."</td>";
    print "<td>" .$res->Field3."</td>";
    print "<td>" .$res->Field4."</td>";
    print "<td>" .$res->Field5."</td>";
    print "<td>" .$res->Field6."</td>";
    print "</tr>";
    }
    ?>
} else {
    echo "<strong>No Results</strong>"
}

Upvotes: 1

Views: 2070

Answers (3)

joshpj1
joshpj1

Reputation: 186

Your if loop is outside the php section, so it won't be evaluated

Upvotes: 0

lu5er
lu5er

Reputation: 3564

Here's an approximate example, to help you out.

<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Select Database
mysqli_select_db($conn, "test");
?>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body bgcolor="#06160F">
            <table frame="hsides" style="color:#ffffff;">
               <!-- Headers of the table -->
                <tr>
                        <th>Header 1 </th>
                        <th>Header 2 </th>
                        <th>Header 3 </th>
                        <th>Header 4 </th>
                        <th>Header 5</th>
                        <th>Header 6 </th>
                </tr>
            </table>
        </div>

        <div>
            <div>
                <table border="1" width="960px">
                    <?php
                    $sql = "SELECT * FROM abc";

                    $result = mysqli_query($conn,$sql);
                    if($result)
                    {
                        if ($result->num_rows > 0) 
                        {echo"<table>";
                         // output data of each row
                         while($row = $result->fetch_assoc()) 
                         {
                             echo"<tr>";
                             echo"<td width=120px align=\"center\">".$row["feild1"]."</td>";
                             echo"<td width=170px align=\"center\">".$row["feild2"]."</td>";
                             echo"<td width=120px align=\"center\">".$row[""feild3"]."</td>";
                             echo"<td width=170px align=\"center\">".$row["feild4"]."</td>";
                             echo"<td width=420px align=\"center\">".$row["feild5"]."</td>";
                             echo"<td width=420px align=\"center\">".$row["feild6"]."</td>";
                             echo"</tr>";
                         }
                         echo"</table>";
                        }
                        else 
                            echo "<h3 align=\"center\">Nothing to show.</h3>";
                    }
                    else
                        echo "<br> Database error.";
                    $conn->close();     
                    ?>
                </table>
            </div>
            <br>
        </div>
    </body>
</html>

By the way I'm using MySQL.

Upvotes: 1

bee
bee

Reputation: 81

You can also check this answer: Checking for empty result (php, pdo, mysql)

I think also, you should use PDO.

Upvotes: 0

Related Questions