TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 01-01-2008, 04:01 AM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default Singleton Help

Hello,

Salathe was helping me a bit with this script I'm coding, and I figured it would be best if I just used a database class for this script, as it might be helpful to us in the future. The problem is, when I use $db->query('query'); it says that query() is an undefined method, when it clearly is.

Now, I'm not even sure if the way I'm going about using the class is the best way, so if someone could make some suggestions, that would be helpful.

Database.class.php
PHP Code:
<?php

class Database {

    private 
$szHost;
    private 
$szUser;
    private 
$szPass;
    private 
$szName;
    private static 
$pInstance;
    
    private function 
__construct($szHost$szUser$szPass$szName) {
        
$this->szHost $szHost;
        
$this->szUser $szUser;
        
$this->szPass $szPass;
        
$this->szName $szName;
        
mysql_connect($this->szHost$this->szUser$this->szPass)
            or die(
'Could not connect to the database.<br />'."\n".mysql_error());
        
mysql_select_db($this->szName)
            or die(
'Could not connect to the database.<br />'."\n".mysql_error());
    }
    
    public static function 
getInstance($szHost$szUser$szPass$szName) {
        if (!
self::$pInstance) {
            
self::$pInstance = new Database($szHost$szUser$szPass$szName);
        }
        return 
self::$pInstance;
    }
    
    public function 
query($szQuery) {
        
$pQuery mysql_query($szQuery);
        return 
$pQuery;
    }
    
}

?>
Excerpt of Categories.class.php
PHP Code:
<?php

class Categories {

    public 
$cat_id;
    public 
$cat_name;
    
    function 
list_cats($szBefore ''$szAfter '') {
        global 
$settings$db;
        
$pQuery $db->query("    SELECT *
                                FROM categories"
);
        
$aResult mysql_fetch_array($pQuery);
        
$szOutput .= $szBefore;
        
$szOutput .= '<a href="'.$settings->info('site_url').'" title="'.$aResult['name'].'">'.$aResult['name'].'</a>';
        
$szOutput .= $szAfter."\n";
        echo 
$szOutput;
    }
    
}

?>
Excerpt of global.php
PHP Code:
<?php

require_once 'config.php';
require_once 
'database.class.php';

function 
__autoload($szClassName) {
    require_once 
strtolower($szClassName).'.class.php';
}

global 
$db$settings$cat;

$db Database::getInstance($dbHost$dbUsername$dbPassword$dbName);
$settings = new Settings;
$cat = new Categories;

?>
Now, the main problem is when I call $cat->list_cats(); It spits out:
Code:
Fatal error: Call to undefined method Database::query() in C:\xampp\htdocs\PHP\VW\includes\categories.class.php on line 10
and I'm not sure why. Does it have to do with how I'm using the globals to gain access to the class? I'm new to the whole database-inclusion bit, so any help would be great.

Thanks,
Andrew
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:44 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design