2017-05-14 17 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 
} 

EDIT
これはリンクと同じように見えた私はそれを設定していますどのようにあるコメントで

<?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を使用していました--HTMLはPHPコードタグの中で実行できますか? – 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を使って接続を成功させることができませんでした。私はJoomlaの記事の中でこの構文を実行しています。 - Sourcererエクステンションを使用 – BellHopByDayAmetuerCoderByNigh

+0

PDOにはどのような問題がありますか?私はこの例でPDOを使用することを学びました。http://www.mustbebuilt.co.uk/php/select-statements-with-pdo/そして問題は一度もありませんでした。 – bee

0

あなたのifループはPHPセクションの外にあるので評価されません

関連する問題