-1

I created a db class that have a methotod to handle the connection to the database.
Now I'm using this method in other script to fetch data from the SQL DB and display in HTML table.

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 - this the script that will display the data in HTML table

<?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>
smarber
  • 4,829
  • 7
  • 37
  • 78
  • You have a typo and because of that your script is not recognized as php: ` php` should be `` sign in the constructor. In the browser's source you will see the complete db script. – jeroen Mar 09 '17 at 11:58
  • THANK YOU! It was a typo... 4 hours spend in trying to fix just for a typo. Now I get this message:Fatal error: Uncaught Error: Call to undefined method db::close() in D:\XAMP\htdocs\telepol\userInfo.php:16 Stack trace: #0 D:\XAMP\htdocs\telepol\userInfo.php(43): showData() #1 {main} thrown in D:\XAMP\htdocs\telepol\userInfo.php on line 16 Can you PLEASE tell me how to fix it? – asdad adasd Mar 09 '17 at 12:08
  • Fixed it. I made a method to close the connection. – asdad adasd Mar 09 '17 at 12:15

1 Answers1

0

That line with <? php in db.php - there shouldn't be space between them

Mathew
  • 89
  • 7