This my 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;
?>
And this is main php file (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();
?>
But when i run this code I see this error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\AppServ\www\ilk\class\header.class.php on line 16
What is the problem?
Your $connect
variable is a parent class. You need to point to the $link
variable it contains.
Change:
$query = mysql_query($sql, $connect) or mysql_error();
to
$query = mysql_query($sql,$link) or mysql_error();
The variable $connect
is not in the scope of the class. Either you leave it out and mysql_query
chooses the last available resource. Or you can pass-through your $connect
variable into the class:
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();
I also want to mention that you should look into Design Patterns, because it's just some pseudo OOP which you are creating.