2017-03-08 55 views
-1

你好,我做了PHP类为我的数据库,我使用的XAMPP在Windows 10PHP数据库类,或使用类的代码是不工作

似乎什么是错的,我不能发现问题,我不知道它在我的类文件或我inex.php文件

这做的是错误:

致命错误:未捕获的错误:类“DB”在d未找到: \ XAMP \ htdocs中\ telepol \ userInfo.php:5堆栈跟踪:#0 d:\ XAMP \ htdocs中\ telepol \ userInfo.php(43):showData()#1 {主}抛出d:\ XAMP \ htdocs中\第5行telepol \ userInfo.php

db.php中

<? php 
class db{ 
    private $_conn; 
    private $_host = "localhost"; 
    private $_username = "root"; 
    private $_psw = ""; 
    private $_dbName = "users"; 

    public function __construct(){ 
    $this->_conn = new mysqli($this->_host, $this->_username, $this->_psw, $this->_dbName); 

    if ($this->_conn->connect_error){ 
     die("Connection failed: " . $this->_conn->connect_error); 
    } 
    } 

    public function getCon(){ 
    return $this->_conn; 
    } 
} 
?> 

userInfo.php

<?php 
require_once 'classes/db.php'; 

function showData(){ 
    $conn = new db(); 
    $br = 1; //br is used to displat the number of each row in the table. 

    $userQuery = "SELECT* FROM users"; 
    $result = $conn->getCon()->query($userQuery); 
    if ($result->num_rows > 0) { 
    while ($row = $result->fetch_assoc()) { 
     echo "<tr> <td>" .$br ."</td> <td>" .$row["first_name"] ."</td> <td>" .$row["last_name"] ."</td> <td>" .$row["nickname"] ."</td> <td>" .$row["user_id"] ."</td> <br>" ; 
     $br ++; 
    } 
    } 
    $conn->close(); 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Users Info</title> 
    <!-- adding JS scripts --> 
    <script type="text/javascript" src="jquery-3.1.1.min.js"></script> 
    <!-- adding css --> 
    <link rel="stylesheet" href="css/bootstrap.css"> 
    <link rel="stylesheet" href="css/bootstrap.min.css"> 
    <!-- my CSS --> 
    <link rel="stylesheet" href="css/style.css"> 
    </head> 
    <body> 
    <table class="table table-striped table-bordered table-hover table-sm"> 
     <thead class="thead-default"> 
     <tr> 
      <th>#</th> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Nickname</th> 
      <th>User ID</th> 
     </tr> 
     </thead> 
     <?php showData(); ?> 
    </table> 
    </body> 
</html> 
+0

我不知道,如何解决它。我试过 '$ database = new db(); $ conn = $ database-> getCon(); ' 还是老样子不工作 –

+1

这可能是因为你的'__construct'功能是私人在'db.php' –

+1

在这里其他方面的建议以及实例您的mysqli类,当你有一个错字。你有msqli,应该是mysqli,你错过了“y”。 – Jase

回答

-1

你$ conn变量是不连接,但一个包装,而您的连接是:$ conn-> getCon()

所以,你可以使用:

$result = $conn->getCon()->query($userQuery); 
+0

另外:你的建筑应该是公共的谢谢@haslam –

+0

我刚刚试过,先生,仍然没有工作。 注意:未定义变量:CONN在d:\ XAMP \ htdocs中\ telepol \ formProcess.php第21行 致命错误:未捕获的错误:调用一个成员函数getCon()在d空:\ XAMP \ htdocs中\ telepol \ formProcess.php:21堆栈跟踪:#{0}主扔在d:\ XAMP \ htdocs中\ telepol \ formProcess.php第21行 –

+0

formProcess.php线21查阅情况,以$康恩,而它并没有被实例化该背景。写$ conn = new db();还有 –