2017-03-09 7 views
-1

データベースへの接続を処理するためのメソトードを持つdbクラスを作成しました。
他のスクリプトでこのメソッドを使用して、SQL DBからデータをフェッチしてHTMLテーブルに表示しています。PHPスクリプトが必要なクラスを見つけられません

Error Message: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; } } ?> Fatal error: Uncaught Error: Class 'db' not found in D:\XAMP\htdocs\telepol\userInfo.php:5 Stack trace: #0 D:\XAMP\htdocs\telepol\userInfo.php(43): showData() #1 {main} thrown in D:\XAMP\htdocs\telepol\userInfo.php on line 5

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 - HTMLテーブル内のデータが表示されます。このスクリプト

<?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

あなたは誤字があります。そのため、あなたのスクリプトはphpとして認識されません: '<? phpは '<?php'でなければなりません。そのため、コンストラクタの最初の '>'記号の後にコードがエコーアウトされているのが見えます。ブラウザのソースに完全なdbスクリプトが表示されます。 – jeroen

+0

感謝します!それはタイプミスだった... 4時間は、ミスを修正するために過ごした。 致命的なエラー:キャッチされていないエラー:定義されていないメソッドdb :: close()をD:\ XAMP \ htdocs \ telepol \ userInfo.phpで呼び出します:16スタックトレース:#0 D:\ XAMP \ htdocs \ telepol \ userInfo.php(43):showData()#1 {main}は、D:\ XAMP \ htdocs \ telepol \ userInfo.phpに16行目でスローされました。 修正方法を教えてください。 –

+0

修正済みです。私は接続を閉じる方法を作った。 –

答えて

0

をdb.php内<? phpでそのライン - そこshouldnそれらの間にスペースがありません

+0

4時間はタイプミスに費やします... –

+0

初めはラフかもしれません:) – Mathew

関連する問題