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 07-14-2009, 03:20 PM   #1 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default Learning OOP the hard way! Please help

Hi,
I am designing my own CMS which i hope to release to the public, however this is my first project I have done using OOP...

Here in Australia there is NO one Tafe or University course that teaches OOP, I am wondering if someone would be kind to help me find out why the following is not working

I have the following files
config.php
dbopen.php
dbclose.php
index.php
articals.php

Now to explain what these files do.
Config.php
- has all the same things that Joomla has inside its config files. Just names a bit different and something left out, but looks the same.

dbopen.php
has the following code
PHP Code:
<?php
require_once("configuration.php");
$vconfig = new VConfig();

    
//Create objects from DBObject class.
    
$myObj = new DBConnect("localhost","$vconfig->user","$vconfig->dbpass","$vconfig->db");
    
/*
        You need to pass the values to the arguments for the constructor from 
        superclass DBConnect. If not $dbData won't know how to connect to database.
    */
   // $dbData = new DBData("localhost","$vconfig->user","$vconfig->dbpass","$vconfig->db");
    
    /*
        You need to connect to MySQL before work on any databases.
    */
    
$myObj ->connectToMySQL();
    
    
/*
        - Because the class DBData defined only one method selectDB(); 
        - Whenever you want to work with a database, you need to connect to MySQL first.
        - In your situation, You didn't call method connectToMySQL() from superclass DBObject.
        --> That's why You cannot select any databases.(Thats why you've got the errors)
    */
    //$dbData ->selectDB();

    
class  DBConnect{
        
/////////////////////////////////////////////////
        // PROTECTED PROPERTIES  
        /////////////////////////////////////////////////
        
protected $hostname$username$password,$db_name$con ;
        
     
/**
       * Constructor
       * @param String $hostname,$username,$password,$db_name. 
       * All information we need to provide whenever connect to db.
       */
        
public function __construct($hostname,$username,$password,$db_name){

            
$this->hostname $hostname;
            
$this->username $username;
            
$this->password $password;
            
$this->db_name $db_name;

        }
        
     
/**
       * Connect to MySQL.
       * @return void
       */
        
public function connectToMySQL(){
            
$this->con mysql_connect($this->hostname,$this->username,$this->password);
            if(
$this->con){
                echo 
"<br>Connected sucessfully to MySQL .<br>";
            }
            else{
                die( 
"<br>Could not connect to MySQL" mysql_error() . ".<br />");
            }
        }
    }      
 
?>
The next file that runs is artical.php

PHP Code:
<?php
require_once("configuration.php");
$vconfig = new VConfig();

$dbData = new DBData("localhost","$vconfig->user","$vconfig->dbpass","$vconfig->db");


class 
DBData extends DBConnect{
        
/* This class doesn't define any constructors. Whenever you create a object from  
         * this class, The object will automaticaly refer to constructor of 
         * super class (DBObject class). cause DBData extends from DBConnect class 
         */ 
        
        
public function title(){
            
$result=$DBData->query('SELECT * FROM push_content');
            
$row=$result->fetch_assoc();
            
//echo user's password
            
print $row['title'];
           
        }
        
        
    }
    

?>
and the closedb.php has the following
PHP Code:
   <?php
   
     
/*     You don't need to create a class just for closing a connection. 
     *    This method you can build  in DBConnect or DBObject class. But anyhow this is your option
     *    Maybe you have some special reason to have this class.
     */   


    
class DBClose extends DBConnect{
        function 
closeConnection(){
           
mysql_close();
        }
    }
    
?>
and in the index.php it links to each one of these using an include script.

the output in the index.php
PHP Code:
<?Php
//use joomla db class
require_once("configuration.php");
$vconfig = new VConfig();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en-gb"
lang="en-gb" >
<head>
<title><?php print $vconfig->site_title?></title>
<meta name="description" content="<?php print $vconfig->SiteDesc?>" />
<meta name="keywords" content="<?php print $vconfig->SEOKeys?>" />

</head>
<body>
<?php
require_once("include/dbconnect/opendb.php");
require_once(
"system/articals/artical.php");


    
$dbData ->title();


</
body>
</
html>
<?
php
require_once("include/dbconnect/dbclose.php");
?>
the code $dbData ->title();
makes this error. Fatal error: Call to a member function query() on a non-object in /home/desvisa/public_html/beta/system/articals/artical.php on line 15

which means this code
PHP Code:
    $result=$DBData->query('SELECT * FROM push_content'); 
is wrong but i dont know why,
can someone please help

Last edited by codefreek : 07-14-2009 at 06:59 PM. Reason: PHP tags added - please read http://www.talkphp.com/lounge/4563-prettifying-pasted-code-talkphp.html
russellharrower 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning things cecilia The Lounge 11 02-19-2013 05:28 AM
Checking bandwidth and hard drive space of a server Orc General 9 04-20-2009 02:21 PM
Lessons/tasks section for those learning PHP Brook Feedback 0 04-11-2009 08:52 PM
Learning and explaining code KingOfTheSouth The Lounge 4 01-09-2009 08:42 PM
Having trouble learning MySQL database codes... Aaron Absolute Beginners 24 05-08-2008 07:11 PM


All times are GMT. The time now is 10:06 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