2017-03-09 1 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 :' '기호 다음에 코드가 출력되는 것을 볼 수 있습니다. 브라우저 소스에서 완전한 db 스크립트를 볼 수 있습니다. – jeroen

+0

감사합니다! 그것은 오타였습니다 ... 4 시간은 오타를 바로 잡으려고했습니다. 다음 메시지가 표시됩니다. 치명적인 오류 : 캐치 오류 : D : \ XAMP \ htdocs \ telepol \ userInfo.php의 정의되지 않은 db :: close() 호출 : 16 스택 추적 : # 0 D : \ XAMP \ htdocs \ telepol \ userInfo.php (43) : showData() # 1 {main}이 (가) D : \ XAMP \ htdocs \ telepol \ userInfo.php에 입력했습니다. 수정 방법을 알려주십시오. –

+0

해결되었습니다. 연결을 끊는 방법을 만들었습니다. –

답변

0

db.php를에 <? php와 그 라인 -이 shouldn 그들 사이에 공간이 없다.

+0

4 시간 동안 오타가 발생했습니다 ... –

+0

시작이 거친 것 같습니다. :) – Mathew

관련 문제