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
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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
Old 07-14-2009, 07:17 PM   #2 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 07-14-2009, 08:20 PM   #3 (permalink)
The Wanderer
 
gregor171's Avatar
 
Join Date: May 2009
Location: Ljubljana, Slovenia
Posts: 9
Thanks: 0
gregor171 is on a distinguished road
Default

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

__________________
WEB Developer
http://xweblabs.com
http://grajzar.info
Send a message via Skype™ to gregor171
gregor171 is offline  
Reply With Quote
Old 07-15-2009, 03:17 AM   #4 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default

Quote:
Originally Posted by gregor171 View Post
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...

Last edited by russellharrower : 07-15-2009 at 03:40 AM.
russellharrower is offline  
Reply With Quote
Old 07-16-2009, 02:25 PM   #5 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default

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

Last edited by codefreek : 07-30-2009 at 03:00 PM. Reason: PHP tags added - please read http://www.talkphp.com/lounge/4563-prettifying-pasted-code-talkphp.html
russellharrower is offline  
Reply With Quote
Old 07-30-2009, 01:57 PM   #6 (permalink)
The Wanderer
 
JaimePinheiro's Avatar
 
Join Date: Jul 2009
Posts: 5
Thanks: 0
JaimePinheiro is on a distinguished road
Default

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 Prettifying Pasted Code on TalkPHP

Last edited by codefreek : 07-30-2009 at 02:58 PM. Reason: PHP tags added - please read http://www.talkphp.com/lounge/4563-prettifying-pasted-code-talkphp.html
JaimePinheiro is offline  
Reply With Quote
Old 07-30-2009, 02:10 PM   #7 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default

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?

Last edited by codefreek : 07-30-2009 at 02: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
Old 07-30-2009, 02:13 PM   #8 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default

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);

?>

Last edited by codefreek : 07-30-2009 at 02: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
Old 07-30-2009, 03:05 PM   #9 (permalink)
The Wanderer
 
JaimePinheiro's Avatar
 
Join Date: Jul 2009
Posts: 5
Thanks: 0
JaimePinheiro is on a distinguished road
Default

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 Prettifying Pasted Code on TalkPHP

Last edited by codefreek : 07-30-2009 at 03:07 PM. Reason: PHP tags added - please read http://www.talkphp.com/lounge/4563-prettifying-pasted-code-talkphp.html
JaimePinheiro is offline  
Reply With Quote
Old 07-30-2009, 03:08 PM   #10 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

Please follow the Rules of code tags..
Thank you,
-CF
codefreek is offline  
Reply With Quote
Reply



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 07:55 AM.

 
     

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