我有这个 php 代码来填充一个表,其中一些数据进入 mysl 数据库, 但是当我执行时它说
"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 脚本新手。
我在教程中使用了这段代码。我不知道“query”和“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 所以现在我必须用数据库第二个条目填充 row2,用数据库第三个条目填充 row3,并增加 html 中的表行数以填充其他数据库条目。 谢谢。
最佳答案
变量区分大小写,($Data->estrai);
使用$data->query将使用MysqlClass类中的查询方法,而不是系统预定义的方法,您需要为其编写一个。
如果您想使用类中的方法,例如 $data->method(),则需要在类中构建它。例如
public function query($sql) {
return mysql_query($sql);
}
此外,我看不到 $this->attiva 的用法,因为您没有在某处将其设置为 TRUE。 $selezione 也没用。
更新:
mysql_select_db 仅返回一个 bool 值,除了报告失败选择(死亡)之外,您不需要它。
它应该是这样的(如果我没有犯任何语法错误..):
<?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;
}
}
更新2:
<?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();
?>
关于php mysql 查询 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22812464/