TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Learning OOP the hard way! Please help (http://www.talkphp.com/advanced-php-programming/4732-learning-oop-hard-way-please-help.html)

russellharrower 07-14-2009 03:20 PM

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

adamdecaf 07-14-2009 07:17 PM

Try:

PHP Code:

$result=$DBData->query('SELECT * FROM ‘push_content‘'); 

I know it's not recommended but he may be having the same problem as me.

gregor171 07-14-2009 08:20 PM

this was really a quick look into code:
$result=$DBData->query...
$DBData is not an object is what the error says.
It should be something like (check the rest of the code):
$result = $this->DBData->query...
but you need a variable:
protected $DBData;
somewhere...
this variable should be an instance of $myObj...

I also noticed confusion: dbopen.php or opendb.php

;-)

russellharrower 07-15-2009 03:17 AM

Quote:

Originally Posted by gregor171 (Post 27007)
this was really a quick look into code:
$result=$DBData->query...
$DBData is not an object is what the error says.
It should be something like (check the rest of the code):
$result = $this->DBData->query...
but you need a variable:
protected $DBData;
somewhere...
this variable should be an instance of $myObj...

I also noticed confusion: dbopen.php or opendb.php

;-)

Sorry its dbopen.php

mmm i cant get it to work...

russellharrower 07-16-2009 02:25 PM

Ok i have mysqli working, and man got to say alot easier then the old Mysql_Connect

this is my Mysqli code

PHP Code:

$mysqli = new mysqli("localhost""$vconfig->user","$vconfig->dbpass","$vconfig->db);
$result $mysqli->query("SELECT * FROM push_content");

   
$row=$result->fetch_assoc();
   
        print 
$row['title'];


$mysqli->close(); 

now this is my problem
the print $row['title'];
is only printing out one title
and I need it to print All the titles, in a array formate

PHP Code:

$titles = array(=> 'title1''title2''title3'); 

I need away to also make it so it only fetchs the latest title first.
Thanks

JaimePinheiro 07-30-2009 01:57 PM

PHP Code:

//try this way
$mysqli = new mysqli("localhost""$vconfig->user","$vconfig->dbpass","$vconfig->db);
$result $mysqli->query("SELECT * FROM push_content");
$titles = array();
   while(
$row=$result->fetch_assoc($result))
   {
    
$titles[] = $row['title'];
   }
$mysqli->close();

var_dump($titles); 


PHP tags added - please read http://www.talkphp.com/lounge/4563-p...e-talkphp.html

russellharrower 07-30-2009 02:10 PM

Thanks for that Jamie,
I am getting a small error which I don't understand
PHP Code:

Warningmysqli_result::fetch_array() expects parameter 1 to be longobject given in /home/desvisa/public_html/beta/index2.php on line 47
array(0) { } 

Any advice?

russellharrower 07-30-2009 02:13 PM

I got this code to work
PHP Code:

<?php

//try this way
$result $mysqli->query("SELECT * FROM push_content LIMIT 10");
$titles = array();
while(
$row=$result->fetch_array())
{
$titles[] = $row['title'];
}


var_dump($titles);

?>


JaimePinheiro 07-30-2009 03:05 PM

Russel,

I don't know this class Mysqli. What does $mysqli->query return? A statement or a result?
If is a result, the code should work passing back $result into fetch.
Could you post Mysqli class declaration?

PHP Code:

//this should work like this
$result $mysqli->query("SELECT * FROM push_content LIMIT 10");
$titles = array();
while(
$row=$mysqli->fetch_assoc($result)) 
//while($row=$mysqli->fetch_array($result))choose the best option
{
$titles[] = $row['title'];




PHP tags added - please read http://www.talkphp.com/lounge/4563-p...e-talkphp.html

codefreek 07-30-2009 03:08 PM

Please follow the Rules of code tags..
Thank you,
-CF


All times are GMT. The time now is 04:57 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0