php mysql查询致命错误

时间:2014-04-02 13:06:39

标签: php mysql sql

我有这个PHP代码来填充表格,一些数据来到一个mysl db, 但当我执行时说

"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();
?>

当我声明$Data->query时,错误在2行。

也许函数查询不正确,我是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第二个条目填充row2,用db 3条目填充row3,并增加html中的表行数以填充其他数据库条目。 感谢。

2 个答案:

答案 0 :(得分:0)

变量区分大小写,($ Data-&gt; estrai);
使用$ data-&gt;查询将使用MysqlClass类中的查询方法,而不是系统预定义的查询方法,您需要为其编写一个。 如果你想使用你的类中的方法,比如$ data-&gt; method(),你需要在你的类中构建它。例如

public function query($sql) {

    return mysql_query($sql);

}

此外,我无法看到$ this-&gt; attiva的使用情况,因为您没有在某处将其设置为TRUE。 $ selezione也没用。

<强>更新

mysql_select_db只返回一个布尔值,除了报告失败选择(die)之外,你不需要它。
它应该是这样的(如果我没有任何语法错误..):

<?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();

?>

答案 1 :(得分: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是区分大小写的语言。

希望它有所帮助。