2017-05-14 43 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部分之外,所以它不會被評估