这是我的dbconnect.class.php
<?php
class Connect
{
//public $error;
public $db;
public function __construct()
{
$link = mysql_connect("localhost","root","1");
$db = mysql_select_db("tarih",$link);
$this->db = $db;
}
}
$connect = new Connect();
$connect = $connect->db;
?>
这是主要的php文件(header.class.php)
<?php
require_once ('dbconnect.class.php');
class Header extends Connect
{
public $headers = array();
public function __construct()
{
/*
* Bu sınıf sayfaların header bilgilerini işler.
*/
}
public function sayfaHeader($sayfa = true)
{
$sql = "SELECT * FROM header WHERE id='" . $sayfa . "'";
$query = mysql_query($sql,$connect) or mysql_error();
echo $sql;
}
}
$header = new Header();
echo $header->sayfaHeader();
?>
但是当我运行此代码时,我看到了这个错误:
警告:mysql_query():提供的参数不是有效的MySQL-Link 第16行的C:\ AppServ \ www \ ilk \ class \ header.class.php中的资源
有什么问题?
答案 0 :(得分:3)
变量$connect
不在类的范围内。要么将其遗漏,mysql_query
选择最后一个可用资源。或者您可以将$connect
变量传递到课程中:
require_once ('dbconnect.class.php');
class Header extends Connect
{
public $headers = array();
protected $database;
public function __construct($database)
{
/*
* Bu sınıf sayfaların header bilgilerini işler.
*/
$this->database = $database;
}
public function sayfaHeader($sayfa = true)
{
$sql = "SELECT * FROM header WHERE id='" . $sayfa . "'";
$query = mysql_query($sql,$this->database) or mysql_error();
echo $sql;
}
}
$header = new Header($connect); // here you pass-through your resource
echo $header->sayfaHeader();
我还想提一下你应该看看Design Patterns,因为它只是你正在创建的一些伪OOP。
答案 1 :(得分:0)
您的$connect
变量是父类。您需要指向它包含的$link
变量。
变化:
$query = mysql_query($sql, $connect) or mysql_error();
到
$query = mysql_query($sql,$link) or mysql_error();