PHP选择SQL视图无输出

时间:2015-04-04 20:44:39

标签: php sql timeout views mysql-connect

我创建了一个从SQL数据库中提取视图的php文件。有人能告诉我为什么这不起作用吗?这似乎是暂时的。我也没有收到连接错误。提前谢谢。

<?php       
require ('mysqli_connect.php');         

    $sql = "SELECT * FROM testview ;";
    $result = mysqli_query($dbc,$sql);

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

     $result=mysqli_query($sql);

     if ($result->num_rows > 0) {
         echo "<table><tr><th>userID</th><th>first_name</th></tr>";
         // output data of each row
         while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["userID"]."</td><td>".$row["first_name"]."</td></tr>";
}
  echo "</table>"; } else {
  echo "0 results"; }

} 
$dbc->close(); 

?>

这是连接文件

<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Test');

// Make the connection:
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' );
?>

2 个答案:

答案 0 :(得分:1)

放手一搏。你在同一个文档中使用mysqli和mysql。这个somtime导致问题。

require_once ('mysqli_connect.php');         

$q = "SELECT * FROM testview";
$r = mysqli_query($dbc, $q);

//there was no real need to check the connection, you should be doing this in your connection script.

//you where using 'mysqli' above and 'mysql' below. 
$row = mysqli_fetch_array($r);

if ($r) {

   echo "<table><tr><th>userID</th><th>first_name</th></tr>";

     while ($row = mysqli_fetch_array($r)){

         echo "<tr><td>" . $row["userID"] . "</td><td>" . $row["first_name"] . " " . $row["last_name"] . "</td></tr>";
     }
         echo "</table>"; 
} else {
         echo "0 results"; 
}

close($conn);

答案 1 :(得分:0)

您没有启用错误消息。因此,当您遇到语法错误等时,它们没有显示出来并且您正在假设超时。或者也许因为你在die()调用中有一个不存在的函数,也许它会因为死而感到困惑而不能死。

在您安装代码之前,请打开错误消息。你的代码会感谢你。

哦,改变

die("Connection failed: " . $dbc->connect_error);

die("Connection failed: " . mysql_error($dbc));