[英]php mysql query fatal error
i have this php code to fill a table with some data come to an mysl db, but when i exec it said 我有这个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();
?>
The error is in 2 line when i declare the $Data->query
. 当我声明$Data->query
时,错误在2行中。
maybe the function query is not correct, i'm new to php scripting. 也许函数查询不正确,我是php脚本的新手。
I've taken this code in a tutorial. 我已在教程中使用了此代码。 i don't know if the "query" and "estrai" (extract) key is correct. 我不知道“查询”和“ estrai”(提取)键是否正确。
Here is the config file: 这是配置文件:
<?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 So now i must to fill row2 with db 2nd entry and row3 with db 3rd entry, and increase number of table row in html to fill other db entries. http://imageshack.com/a/img35/1966/pd0v.png所以现在我必须用db 2nd条目填充row2并用db 3rd条目填充row3,并增加html中的表行数以填充其他db条目。 thanks. 谢谢。
Variables are case sensitive, ($Data->estrai); 变量区分大小写,($ Data-> estrai);
Using $data->query will use the query method from your MysqlClass class, not the system predefined one, you you need to write one for it. 使用$ data-> query将使用MysqlClass类中的查询方法,而不是系统预定义的方法,您需要为其编写一个方法。 If you want to use the method from your class like $data->method(), you need to build it in your class. 如果要使用类中的方法,例如$ data-> method(),则需要在类中进行构建。 For example 例如
public function query($sql) {
return mysql_query($sql);
}
Also I can't see the usage of $this->attiva since you didn't set it to TRUE somewhere. 我也看不到$ this-> attiva的用法,因为您没有在某处将其设置为TRUE。 $selezione is also useless. $ selezione也没用。
UPDATES: 更新:
mysql_select_db only returns a boolean value, you don't need it other than reporting a fail select (die). mysql_select_db只返回一个布尔值,除了报告失败选择(die)外,您不需要它。
It should be something like this (if I didn't make any syntax mistake..): 应该是这样的(如果我没有犯任何语法错误..):
<?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: 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();
?>
In the specific line you have an error: 在特定行中,您有一个错误:
$post_sql = $data->query("SELECT * FROM products ORDER BY id DESC");
Change to this: 更改为此:
$post_sql = $Data->query("SELECT * FROM products ORDER BY id DESC");
The variable $Data needs to be Uppercase all time (because you created it like that), PHP is a Case sensitive language. 变量$ Data始终都必须为大写(因为您是这样创建的),PHP是区分大小写的语言。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.