Go Back   Cyber Tech Help Support Forums > Software > Web Development & Graphic Design

Notices

Web Development & Graphic Design Problem Solving for Graphic Design, PHP, ASP, Perl, MySQL, SQL, XML, HTML issues

Reply
 
Topic Tools
  #1  
Old March 18th, 2008, 07:07 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
Question class scope

manual

PHP Code:
class database{
    function 
__construct(){
          
$this->connect_id mysql_connect('localhost','root','');
            if(
$this->connect_id){
                if (
mysql_select_db('dbname')) return $this->connect_id;
                else return 
$this->error();   
            }else return 
$this->error();
    }
    function 
query($query){
        if (
$query != NULL){
            
$this->query_result mysql_query($query$this->connect_id);
            if(!
$this->query_result){
                return 
false;
            }else{
                return 
$this->query_result;
            }
        }else{
            return 
false;
        }
    }

I want to access query from other class like this:

PHP Code:
    database::query("UPDATE users SET ...."); 
but I can't access it, any ideas? I think I should update database class because I read somewhere that I can't access $this-> from other class..

edit: I know about this solution
PHP Code:
$db = new database();
$db->query... 
and it works, but I don't know, I don't like when I need to "recreate" class..

Last edited by FrEaKmAn; March 18th, 2008 at 07:12 PM.
Reply With Quote
  #2  
Old March 19th, 2008, 10:24 AM
oracle128's Avatar
oracle128 oracle128 is offline
Senior Member
 
Join Date: Oct 2000
O/S: Windows XP Pro
Location: Melbourne, Australia
Age: 40
Posts: 9,401
If you don't instantiate the class, the constructor won't execute. Your class has make the database connection in the constructor. Even if you got your code to access database::query, it wouldn't work anyway. In fact, depending on what error you're getting, this may be the problem itself.
Reply With Quote
  #3  
Old March 19th, 2008, 01:37 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
this works

PHP Code:
class database{
    function 
__construct(){
          
$this->connect_id mysql_connect('localhost','root','');
            if(
$this->connect_id){
                if (
mysql_select_db('dbname')) return $this->connect_id;
                else return 
$this->error();   
            }else return 
$this->error();
    }
    function 
query($query){
        if (
$query != NULL){
            
$this->query_result mysql_query($query$this->connect_id);
            if(!
$this->query_result){
                return 
false;
            }else{
                return 
$this->query_result;
            }
        }else{
            return 
false;
        }
    }
}  
class 
site extends database {
      function 
__construct(){
           
parent::__construct()
           
parent::query("");
      }


Reply With Quote
Reply

Bookmarks

Topic Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Topic Topic Starter Forum Replies Last Post
Help with security class jennifer24 Networking 0 July 31st, 2007 09:19 PM
Night Class gooner Jokes Forum 0 June 13th, 2007 05:37 PM
Programming class ggross Open Discussion 3 May 4th, 2007 01:12 PM
Remember Bio class? bAdWaYz Open Discussion 3 July 10th, 2005 02:07 PM
Battery Scope files missing after KlZ virus infection Bert Lesley Windows 98 2 September 25th, 2002 06:05 PM


All times are GMT +1. The time now is 05:56 AM.