2017-05-14 44 views
1

我正在运行一个SQL查询,并获得使用PHP的结果的计数。下面是我的语法的相关部分,我想要做的是如果返回的计数是> = 1,然后显示网格(因为它将填充1行或更多行),但如果返回的行计数为0,则在屏幕上显示通知用户的警告。PHP与行号,如果其他

我的问题与代码是,它总是返回网格!

<?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 
} 

编辑
这是我如何设置它这似乎是一样的评论的链接

<?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>" 
} 
+1

为什么你在'if-else'之前关闭'?>'? – lU5er

+0

我正在使用HTML写出表格 - 可以在PHP代码标记内执行html吗? – BellHopByDayAmetuerCoderByNigh

+0

是的!使用'echo' – lU5er

回答

1

下面是一个近似的例子,以帮助你。

<?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> 

顺便说一下,我使用MySQL。

+0

@BellHopByDayAmetuerCoderByNigh,它工作? – lU5er

0

您还可以检查这个答案:Checking for empty result (php, pdo, mysql)

我想也是,你应该使用PDO。

+0

我从来没有能够使用PDO geta成功连接。我在Joomla文章的文章中运行这个语法。 - 使用Sourcerer扩展 – BellHopByDayAmetuerCoderByNigh

+0

PDO有什么问题?我学会了用这个例子http://www.mustbebuilt.co.uk/php/select-statements-with-pdo/来使用PDO,并且从来没有任何问题。 – bee

0

你的if循环在php部分之外,所以它不会被评估