PHP MYSQL数据库连接和显示数据不起作用

时间:2014-04-10 05:40:38

标签: php mysql

我刚接触到php mysql,需要帮助。我无法通过以下代码显示来自mysql数据库的数据。它显示一个空白页:(。任何人都可以帮我吗?下面是我的配置文件和索引文件代码。

的config.php

<?php
class DB {

    protected $db_name = 'drive';
    protected $db_user = 'root';
    protected $db_pass = '';
    protected $db_host = 'localhost';


    public function connect() {
        $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        mysql_select_db($this->db_name);
        return true;
    }


class showClass{
    //USER LIST SHOW function
public function showUser($table){

        $query=mysql_query("SELECT * FROM $table WHERE type='user'") or die(mysql_error()); 
        $data=NULL;
        if(mysql_num_rows($query)>0){
            while($rows=mysql_fetch_assoc($query)){
            $data[]=$rows;
            }
            return $data;
        }else{
            echo '<span class="text-info success">No Account Found.</span>';
        exit();
        }

    }

}

的index.php

<?php
require_once 'conf/config.php';


$db = new DB();
$db->connect();
$obj_show = new showClass();
?>
<html>
<head>
<title>Drive</title>
</head>
<body>

<table class="table table-bordered table-hover" cellpadding="5">

<tr>
<th scope="col"><b>User ID</b></th>
<th scope="col"><b>Name</b></th>
</tr>


<?php

  $allData=$obj_show->showUser("user");
  foreach($allData as $data){
      extract($data);
        echo <<<show

<tr>
  <td>$id</td>
  <td >$username</td>  
</tr>

show;
  }
  ?>

</table>

</body>

3 个答案:

答案 0 :(得分:2)

正如我在评论中已经说过的那样,在类DB 结束后,您将缺少}

答案 1 :(得分:2)

as @Cruel回答你缺少}在课程结束时 DB 尝试改变

class DB {

    protected $db_name = 'drive';
    protected $db_user = 'root';
    protected $db_pass = '';
    protected $db_host = 'localhost';


    public function connect() {
        $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        mysql_select_db($this->db_name);
        return true;
    }
}

答案 2 :(得分:1)

您输出时显示的语法错误在添加ini_set function

时显示

正确的代码如下,您需要在类DB之后添加一个右括号

<?php
class DB {

    protected $db_name = 'drive';
    protected $db_user = 'root';
    protected $db_pass = '';
    protected $db_host = 'localhost';


    public function connect() {
        $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        mysql_select_db($this->db_name);
        return true;
    }

}// MISSING CLOSING BRACKET
class showClass{
    //USER LIST SHOW function
public function showUser($table){

        $query=mysql_query("SELECT * FROM $table WHERE type='user'") or die(mysql_error()); 
        $data=NULL;
        if(mysql_num_rows($query)>0){
            while($rows=mysql_fetch_assoc($query)){
            $data[]=$rows;
            }
            return $data;
        }else{
            echo '<span class="text-info success">No Account Found.</span>';
        exit();
        }

    }

}