0

这是我的 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():提供的参数不是第 16 行 C:\AppServ\www\ilk\class\header.class.php 中的有效 MySQL-Link 资源

问题是什么?

4

2 回答 2

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。

于 2012-05-02T18:10:12.100 回答
0

您的$connect变量是父类。您需要指向$link它包含的变量。

改变:

$query = mysql_query($sql, $connect) or mysql_error(); 

$query = mysql_query($sql,$link) or mysql_error(); 
于 2012-05-02T18:09:55.177 回答