2014-04-02 69 views
0

我有此PHP代碼來填充一個表的一些數據來的思想報分貝, 但是當我exec的它說PHP的MySQL查詢致命錯誤

"Fatal error: Call to undefined method MysqlClass::query()" 


<?php 
include "config.php"; 
$data = new Mysqlclass(); 
$data->connetti(); 
$post_sql = $data->query("SELECT * FROM products ORDER BY id DESC"); 
if(mysql_num_rows($post_sql) > 0) 
    {while($post_obj = $Data->estrai($post_sql)) 
     {$id=$post_obj->id; 
      $name = stripcslashes($post_obj->name); 
      $cat = stripcslashes($post_obj->category); 
      $price= stripcslashes($post_obj->price); 
      echo "<td>".$id."</td>"; 
      echo "<td>".$name."</td>"; 
      echo "<td>".$cat."</td>"; 
      echo "<td>".$price."</td>"; 
     } 
    }else{ 
     echo"Tabella vuota"; 
    } 
    $data->disconnetti(); 
?> 

的錯誤是在2線時,我聲明$Data->query

也許功能查詢不正確,我是新的PHP腳本。

我已經在教程中使用了這段代碼。我不知道「查詢」和「estrai」(提取)鍵是否正確。

下面是配置文件:

<?php 

    class MysqlClass 
    { 
     private $nomehost = "localhost";  
     private $nomeuser = "root";   
     private $password = "xxxxx"; 
     private $nomedb = "intse"; 
     private $attiva = false; 
     public function connetti() 
     { 
     if(!$this->attiva) 
     { 
      if($connessione = mysql_connect($this->nomehost,$this->nomeuser,$this->password) or die (mysql_error())) 
      { 
      $selezione = mysql_select_db($this->nomedb,$connessione) or die (mysql_error()); 
      } 
     } else{ 
      return true; 
     } 
     } 
     public function disconnetti() 
     { 
     if($this->attiva) 
     { 
      if(mysql_close()) 
      { 
      $this->attiva = false; 
      return true; 
      } else { 
      return false; 
      } 
     } 
     } 
    } 
    ?> 

http://imageshack.com/a/img35/1966/pd0v.png 所以現在我必須填寫與DB 2日進入和ROW3與DB 3項2行,並增加HTML表格的行數,以填補其他數據庫條目。 謝謝。

+0

您與db的連接失敗。你需要弄清楚爲什麼。 –

+3

不知道沒有看到類文件 – Steve

+0

謝謝我會檢查配置,也許mysql不接受我的連接 – andreyo

回答

0

變量區分大小寫,($ Data-> estrai);
使用$ data-> query將使用MysqlClass類中的查詢方法,而不是系統預定義的查詢方法,您需要爲其編寫一個查詢方法。 如果你想像你的類一樣使用$ data-> method()方法,你需要在你的類中構建它。例如

public function query($sql) { 

    return mysql_query($sql); 

} 

此外,我不能看到$ this-> attiva的用法,因爲您沒有將它設置爲TRUE的地方。 $ selezione也沒用。

更新:

mysql_select_db只返回一個布爾值,你不需要它除了報告失效選擇(模具)。
它應該是這樣的(如果我沒有做任何語法錯誤。):

<?php 

include 'config.php'; 

$data = new MysqlClass(); 
$data->connetti(); 

$post_sql = $data->query("SELECT * FROM products ORDER BY id DESC"); 

if(mysql_num_rows($post_sql) > 0) { 

    while($post_obj = $data->estrai($post_sql)) { 

     // the data it returns is an array, not object 
     // so it should be $post_obj['key'] not $post_obj->key 

     $id = $post_obj['id']; 
     $name = stripcslashes($post_obj['name']); 
     $cat = stripcslashes($post_obj['category']); 
     $price = stripcslashes($post_obj['price']); 

     echo "<td>".$id."</td>"; 
     echo "<td>".$name."</td>"; 
     echo "<td>".$cat."</td>"; 
     echo "<td>".$price."</td>"; 
    } 

} else { 

    echo 'Tabella vuota'; 

} 

$data->disconnetti(); 

?> 

<?php 

class MysqlClass { 

    private $nomehost = 'localhost';  
    private $nomeuser = 'root';   
    private $password = 'xxxxx'; 
    private $nomedb = 'intse'; 
    private $attiva = FALSE; 
    private $connessione; 

    public function connetti() { 

     if(!$this->attiva && ($this->connessione = mysql_connect($this->nomehost, $this->nomeuser, $this->password) or die(mysql_error()))) { 

      mysql_select_db($this->nomedb, $this->connessione) or die(mysql_error()); 
      $this->attiva = TRUE; // Now this becomes useful, because the value has been set 

     } 

     return $this->attiva; 

    } 

    // Method to build up the query 

    public function query($sql) { 

     return mysql_query($sql, $this->connessione); 

    } 

    // Method to extract information from the query 

    public function estrai($query) { 

     return mysql_fetch_array($query, MYSQL_ASSOC); 

    } 

    public function disconnetti() { 

     // More straight forward way to return the status 

     if($this->attiva) return mysql_close($this->connessione); 

     return FALSE; 

    } 

} 

UPDATE2:

<?php 

include 'config.php'; 

$data = new MysqlClass(); 
$data->connetti(); 

$post_sql = $data->query("SELECT * FROM products ORDER BY id DESC"); 

echo '<table>'; 

if(mysql_num_rows($post_sql) > 0) { 

    while($post_obj = $data->estrai($post_sql)) { 

     // the data it returns is an array, not object 
     // so it should be $post_obj['key'] not $post_obj->key 

     $id = $post_obj['id']; 
     $name = stripcslashes($post_obj['name']); 
     $cat = stripcslashes($post_obj['category']); 
     $price = stripcslashes($post_obj['price']); 

     echo '<tr>'; 

     echo "<td>".$id."</td>"; 
     echo "<td>".$name."</td>"; 
     echo "<td>".$cat."</td>"; 
     echo "<td>".$price."</td>"; 

     echo '</tr>'; 

    } 

} else { 

    echo 'Tabella vuota'; 

} 

echo '</table>'; 

$data->disconnetti(); 

?> 
+0

對不起,但我是小菜鳥的PHP,如果可能的話,你可以進行修改並重新發布文件? $ selezione(select)做dbname的選擇,是不正確的? – andreyo

+0

@andreyo看到我做的更新 –

+0

非常感謝你,現在我已經完成了表的填充,現在真正的問題是:我在頁面中有很多行,並且在db中有很多行,我必須填滿所有的表格並添加新行以填充頁面中的所有產品,我如何添加新行?如果我將這個代碼複製到新表格行中,它會填充第一個記錄。有一種方法可以添加第二個,第三個ecc .. row? – andreyo

0

在具體的行,你有一個錯誤:

$post_sql = $data->query("SELECT * FROM products ORDER BY id DESC"); 

改成這樣:

$post_sql = $Data->query("SELECT * FROM products ORDER BY id DESC"); 

變量$ Data必須始終爲大寫(因爲您是這樣創建的),PHP是區分大小寫的語言。

希望它有幫助。