Jump to content

[SOLVED] Using other classes in a class


papaface

Recommended Posts

Hello,

I was wondering if someone can help me.

I have this code:

<?php
session_start();
include("includes/connect.php");
include("includes/page.class.php");

$page = new page;
$page->buildHeader("BadMembers - Homepage");
?>

In includes/connect.php I have a class:

<?php

include_once "ez_sql_core.php";
include_once "ez_sql_mysql.php";
$db = new ezSQL_mysql('root','','bm','localhost');

?>

How can I use $db->query(SOMESQL) in my includes/page.class.php:

<?php
class page
{

	function buildHeader ($title)
		{
		include("templates/header.php");
		}

}
?>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/
Share on other sites

Something like this

<?php

class page
{
    private $db;
        
        function __construct ($db)
            {
                $this->db = $db;
            }
            
	function buildHeader ($title)
		{
		include("templates/header.php");
		}

}
    

?>

 

Then

<?php
session_start();
include("includes/connect.php");
include("includes/page.class.php");

$page = new page($db);                         // pass db to the page class
$page->buildHeader("BadMembers - Homepage");
?>

 

Alternatively, you could have a "setDB()" method in the page class and pass the $db via that instead of in the constructor.

Hmm just tried this and I get the following error:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in includes\register.class.php on line 32

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in includes\register.class.php on line 32
0

 

This is the code I have (bare with me please):

register.class.php:

<?php
class register
{
var $db;

	function setDB($_db)
		{
		$this->db = $_db;
		}

		function buildForm()
			{
			include("templates/register.form.php");
			}

		function confirmPost($submit)
				{
				if 	($_POST['submit'] != "")
					{
					return true;
					}
				else
					{
					return false;
					}
				}	

			function checkForm()
				{
				if	($_POST['username'] != "")
					{
					$var = $this->db->get_var("SELECT count(*) FROM members where username='".mysql_real_escape_string($_POST['username'])."'");

					echo $var;
					}
				}

}
?>

 

register.php:

<?php
session_start();
include("includes/connect.php");
include("includes/page.class.php");
include("includes/register.class.php");

$_register = new register;
$_page = new page;
$_page->buildHeader("BadMembers - Register","");

if	($_register->confirmPost($_POST['submit']))
{
$_register->setDB($_db);
$_register->checkForm();
}
else
{
$_register->buildForm();
}
?>

<?php
$_page->buildFooter();
?>

includes/connect.php:

<?php

include_once "ez_sql_core.php";
include_once "ez_sql_mysql.php";
$_db = new ezSQL_mysql('root','','bm','localhost');

?>

 

I dont think the $_db variable is being passed correctly....

Yeah its definately right as you can see here:

function ezSQL_mysql($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
	{
		$this->dbuser = $dbuser;
		$this->dbpassword = $dbpassword;
		$this->dbname = $dbname;
		$this->dbhost = $dbhost;
	}

As I say the connection DEFINITELY works.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.