Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.