2017-01-10 57 views
0

我在下面粘貼的腳本中收到錯誤。我不知道爲什麼我得到他們並需要幫助。謝謝。使用mysqli和OOP獲取錯誤

error displaying image

mysqli_fetch_assoc-的index.php

<?php 
//mysqli connect here 
include('mysqli_fetch_assoc.php'); 
$newbookings = new booking(); 
?> 
<div class="container"> 
    <table class="table table-hover text-center"> 
     <thead> 
      <tr align="center"> 
       <th>Name</th> 
       <th>Contact</th> 
       <th>Email</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php 
      $newbookings->showBooking(); 

      while($row = mysqli_fetch_assoc($res)){//shows error here 
      ?> 
      <tr> 
       <td><?php $row['name'];?></td> 
       <td><?php $row['contact'];?></td> 
       <td><?php $row['email'];?></td> 
      </tr> 
      <?php 
      } 
      ?> 
     </tbody> 
    </table> 
</div> 

mysqli_fetch_assoc.php:

<?php 
class booking{ 
    function __construct(){ 
     $mysqli = mysqli_connect('localhost','root','admin','homestay'); 

     if(mysqli_connect_errno($mysqli)){ 
      echo "Connection failed".mysqli_connect_errno; 
     } 
    } 

    function showBooking(){ 
     $res = mysqli_query($mysqli,"SELECT * FROM bookmyroom");//shows error here 
     return $res; 
    } 
} 
?> 
+0

你可能需要做'$水庫= $ newbookings-> showBooking();',而不是僅僅'$ newbookings-> showBooking();' – Rasclatt

+1

你還需要將'$ mysqli'公開並將其作爲'$ this-> mysqli'在外部以$ newbookings - > $ mysqli'之類的形式傳遞。可能是像getConnection()這樣的方法,它將返回'$ this - > $ mysqli',這可能是私有的。 – Rasclatt

回答

-1

正如我評論,下面是具體在哪裏,我指的是用符號:

<?php 
class booking 
    { 
     # Create variable to pass inside the class 
     private $mysqli; 
     # I would look into dependency injection of the database instead of 
     # creating connection right inside this class 
     public function __construct() 
      { 
       #Store the connection to share with the rest of the class 
       $this->mysqli = mysqli_connect('localhost','root','admin','homestay'); 
       # I would also check this whole statement here, 
       # I don't think this is right. Try this. 
       # Here is the page for this usage: http://php.net/manual/en/mysqli.connect-error.php 
       if(!$this->mysqli) { 
        # You have to add "()" here------------------vv 
        echo "Connection failed".mysqli_connect_errno(); 
       } 
      } 

     public function showBooking() 
      { 
       # You get an error here because $mysqli is out of scope, but 
       # using $this->mysqli would be fine 
       return mysqli_query($this->mysqli,"SELECT * FROM bookmyroom"); 
      } 
     # create a return method for the connection, especially if you 
     # plan to use the connection outside the class. Again, injection 
     # of a database class is more suitable... 
     public function getConnection() 
      { 
       return $this->mysqli; 
      } 
    } 

在其他頁面,你會怎麼做:

# Since "showBooking()" returns a query, so you need to assign to a variable 
$res = $newbookings->showBooking(); 

我還要指出,這將是更好的通過/注入數據庫類到這個類,而不是創建連接爲__construct()的一部分。另外,我將就連接類型最後一個評論,我個人認爲PDO是更強大,更易於使用,所以如果你開始了,我會重新考慮使用PDO作爲替代mysqli_*



連接

編輯

因爲我有一個時刻,這裏是你可能要交替做什麼:

/defines.php

<?php 
# If you create some defines, you will more easily be able to alter 
# your site structure without major rewriting 
define('DB_HOST','localhost'); 
define('DB_NAME','homestay'); 
define('DB_USER','root'); 
define('DB_PASS','admin'); 
# These are directory helpers 
define('DS',DIRECTORY_SEPARATOR); 
define('ROOT_DIR',__DIR__); 
define('CLASSES', ROOT_DIR.DS.'classes'); 

/config.php

<?php 
# If you create a config file that you include in every page at the top, 
# you can keep your site more easily structured 
# 
# Include the defines to help keep the site consistent 
require(__DIR__.DIRECTORY_SEPARATOR.'defines.php'); 
# Create session here if your site uses sessions 
session_start(); 
# Create a class autoloader so you don't have to manually include class files. 
# It does require that you place your classes in the same folder structure 
spl_autoload_register(function($class) { 
    # This converts your classes into directory paths to the class folder 
    $path = str_replace(DS.DS,DS,CLASSES.DS.str_replace('\\',DS,$class)); 
    # If the file exists, include it 
    if(is_file($path)) 
     include_once($path); 
}); 

/classes/Database.php

<?php 
/* 
** @description Class creates a commonly used database connection 
*/ 
class Database 
    { 
     private static $con; 

     public function connect() 
      { 
       # If there is a connection already made, send back 
       if(!empty(self::$con)) 
        return self::$con; 
       #I would encourage PDO here... 
       self::$con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME); 
       if(!self::$con) { 
        die("Connection failed".mysqli_connect_errno()); 
       } 
       # Send back connection for use 
       return self::$con; 
      } 
    } 

/類/預訂

<?php 
/* 
** @description This class is your booking class that receives the database 
**     to use, so you don't create it here, only use it here. 
*/ 
class Booking 
    { 
     private $con; 
     /* 
     ** @param $con [object] Insert your Database connection here 
     */ 
     public function __construct($con) 
      { 
       $this->con = $con; 
      } 

     public function showBooking() 
      { 
       $row = array(); 
       $query = mysqli_query($this->con,"SELECT * FROM bookmyroom"); 
       if(empty($query)) 
        return $row; 

       while($result = mysqli_fetch_assoc($query)) { 
        $row[] = $result; 
       } 

       return $row; 
      } 
    } 

的index.php

<?php 
# Adding just the config will take care of including classes for you, 
# provided you have set up the classes into a class folder and the autoloader 
# has the correct path in place. This would be at the top of every page 
require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php'); 
# Create the bookings here, pass the database. It may be beneficial to make the 
# connection in the config file for site use, but I don't know your site 
# structure, so that is a guess... 
$con = (new Database())->connect(); 
$newbookings = new Booking($con); 
?> 
<div class="container"> 
    <table class="table table-hover text-center"> 
     <thead> 
      <tr align="center"> 
       <th>Name</th> 
       <th>Contact</th> 
       <th>Email</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php 
      # Since the class already returns an array, just use foreach() 
      foreach($newbookings->showBooking() as $row){ 
      ?> 
      <tr> 
       <td><?php $row['name'];?></td> 
       <td><?php $row['contact'];?></td> 
       <td><?php $row['email'];?></td> 
      </tr> 
      <?php 
      } 
      ?> 
     </tbody> 
    </table> 
</div> 
+1

我很欣賞downvotes和所有,但沒有評論或話語,有什麼意義? – Rasclatt