2013-07-02 43 views
-1

我得到了致命錯誤:在第19行的/config/mySQL_class.php中找不到類'mysqli' 我測試了我的代碼,但似乎無法找到解決我的問題的方法,我只是一個剛開始嘗試OOP的初學者程序員。有任何想法嗎?在OOP中找不到屬性

<?php 


class Database 
{ 



    public $data; 

    public $mysqli; 

    public function __construct($host, $username, $password, $database){ 

     $this->mysqli = new mysqli($host, $username, $password, $database); 

     if(mysqli_connect_errno()){ 

      echo "Error: Could not connect to database."; 

     exit; 

     } 
    } 



    public function __destruct(){ 

     $this->mysqli->close(); 

    } 


    public function read(){ 

    $sql = "SELECT * FROM `user` "; 
    $result = $this->mysqli->query($query); 
    $num_result = $result->num_rows; 

    if($num_result>0) 
    { 

     while($rows=$result->fetch_assoc()){ 

      $this->data[]=$rows; 

       } 
      } else { 

       return $this->data; 

     } 
    } 




} 

?> 

使用

include('config/mySQL_class.php'); 

$obj=new Crud("localhost","root","","crud"); 
$obj->read(); 

    <table width="500" border="1" cellpadding="5"> 
     <tr> 
     <th width="16" scope="row">id</th> 
     <td width="95">name</td> 
     <td width="140">email</td> 
     <td width="104">address</td> 
     <td width="71">Mobile</td> 
     <td>action</td> 
     </tr> 

     <?php 
     foreach($obj->data as $val){ 
      extract($val); 

      ?> 
      <tr> 
      <td scope="row"><?php echo $id; ?></td> 
      <td><?php echo $name; ?></td> 
      <td><?php echo $email; ?></td> 
      <td><?php echo $address; ?></td> 
      <td><?php echo $mob; ?></td> 
      <td><a href="edit.php?id=<?php echo $id; ?>">edit</a>|<a href="delete.php?id=<?php echo $id; ?>">Delete</a></td> 
      </tr> 
      <?php 
     } 

     ?> 
+2

這可能意味着'mysqli'沒有安裝在你的PHP版本中?參見['phpinfo()'](http://php.net/phpinfo); – deceze

+0

我在phpinfo()上找不到mysqli; @deceze有一件事,我可以使用mysql而不是mysqli?謝謝 – Jason

回答

-1

這是當MySQLi擴展是不是你的服務器上安裝一個COMON錯誤。

要檢查MySQLi擴展是否可用,試試這個:

if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) { 
    echo 'MySQLi extension is missing!'; 
} else { 
    echo 'MySQLi extension is installed!'; 
} 

要安裝MySQLi擴展,遵循this instructions

您也可以安裝MySQLi擴展,但在php.ini文件中留言。打開php.ini並檢查是否extension = php_mysqli.so未被評論。如果是這樣,取消註釋並重新啓動Apache服務器。