1

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>"
}
12
  • 1
    why are you closing the ?> before if-else ? Commented May 14, 2017 at 15:17
  • I was using HTML to write out the table - can the html be perfomed inside the php code tag? Commented May 14, 2017 at 15:19
  • yes! use echo Commented May 14, 2017 at 15:20
  • Or put ?> inside the loop in the if block. Commented May 14, 2017 at 15:20
  • @IU5er - I just looked at my code, and for some reason I have the data from sql being written to the table in php code tags and am using the print statement. Is that effectively the same as an echo statement, print that is? Commented May 14, 2017 at 15:22

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

I think also, you should use PDO.

2 Comments

I have never been able to geta succesful connection using PDO. I am running this syntax inside an article on my Joomla article. -- using the Sourcerer extension
What's your problem with PDO? I learned to use PDO with this example mustbebuilt.co.uk/php/select-statements-with-pdo and had never any problems.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.