1

This is dbconnect.class.php:

<?php  

class Connect 
{ 
    public $error; 
    protected $db; 
    public function __construct() 
    { 
        $link = mysql_connect("localhost","root","1") or $this->error = mysql_error(); 
        $db = mysql_select_db("tarih",$link); 
        $this->db = $db; 
    } 
} 
?>

And this is main php file:

<?php
//header.class.php 
require_once 'dbconnect.class.php'; 

class Header extends Connect 
{ 
    public $headers = array(); 
    private $baglan; 
    public function __construct() 
    { 
        /* 
        * Bu sınıf sayfaların header bilgilerini işler. 
        */ 
        $baglan = $this->db; 
    } 

    public function sayfaHeader($sayfa = true) 
    { 
        $sql = "SELECT * FROM header WHERE id='" . $sayfa . "'"; 
        $query = mysql_query($sql,$this->baglan); 
    } 


} 

Header::sayfaHeader(); 
?>

When I run main php file I see this error:

Fatal error: Using $this when not in object context in C:\AppServ\www\ilk\class\header.class.php on line 19  

Line 19:

$query = mysql_query($sql,$this->baglan);  

Where is the problem? I can't see problem becuase I'm not writing php code for so long.

3
  • 1
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. Commented May 2, 2012 at 16:26
  • @Truth this is not problem for mysql_ this problem for object-oridient-programming Commented May 2, 2012 at 16:31
  • 2
    I never said it would solve your problem. If I did thought so, I would have posted it as an answer. I'm just saying you should. Commented May 2, 2012 at 16:37

4 Answers 4

2
Header::sayfaHeader(); 

It calls method without object creation. You have to do this

Header obj = new Header();
obj->sayfaHeader();

If you want to call class's method then declare method as static. But you cant use $this reference because static methods and memebers haven't reference on class object.

UPDATE:
mysql_select_db returns bool value. You have to use your $link variable for querying.

Sign up to request clarification or add additional context in comments.

7 Comments

First question: Why? In a sense, i can't use other method? Second question: now 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 20
Non-static method is property of Object. Static method belongs to class not for class instance.
I answered in my answer under Update
Yes mysql_select_db returns 1. What need mysql_select-db return?
You must use $link variable - MySql-link
|
1

You are not having database issue,

your code is wrong.

Header::sayfaHeader(); 

That is for static methods. but you need an instance to call that method actually.

try:

Header obj = new Header();
obj->sayfaHeader();

2 Comments

question: now I see this error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\AppServ\www\ilk\class
You are using java in my opinion :)
-1

I think you need to construct the parent to instantiate the database class

public function __construct() 
{ 
    parent::__construct();
    $baglan = $this->db; 
} 

1 Comment

Now I can see only: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\AppServ\www\ilk\class\header.class.php on line 20
-1
public function __construct() 
{ 
    // if you do this, add a $this-> to baglan to acces it in the class
    $this->baglan = $this->db; 
} 

public function sayfaHeader($sayfa = true) 
{ 
    $sql = "SELECT * FROM header WHERE id='" . $sayfa . "'"; 

    // or, since you inherit the $this->db
    $query = mysql_query($sql,$this->db); 
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.