papaface Posted September 13, 2007 Share Posted September 13, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/ Share on other sites More sharing options...
Barand Posted September 13, 2007 Share Posted September 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348017 Share on other sites More sharing options...
papaface Posted September 13, 2007 Author Share Posted September 13, 2007 Thanks for that I did that yesterday but I didnt think that was a good way to do it. Obviously I was wrong. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348020 Share on other sites More sharing options...
papaface Posted September 14, 2007 Author Share Posted September 14, 2007 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.... Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348121 Share on other sites More sharing options...
pocobueno1388 Posted September 14, 2007 Share Posted September 14, 2007 Those errors mean that your not connecting to the database successfully. Make sure all your information is correct, it looks like it's not taking your password. Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348123 Share on other sites More sharing options...
papaface Posted September 14, 2007 Author Share Posted September 14, 2007 Its definately correct. Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348126 Share on other sites More sharing options...
papaface Posted September 14, 2007 Author Share Posted September 14, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348150 Share on other sites More sharing options...
Barand Posted September 14, 2007 Share Posted September 14, 2007 Are sure the connect is right $_db = new ezSQL_mysql('root','','bm','localhost'); It's normally host, user, pwd, db Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348259 Share on other sites More sharing options...
papaface Posted September 14, 2007 Author Share Posted September 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348326 Share on other sites More sharing options...
Barand Posted September 14, 2007 Share Posted September 14, 2007 Something weird - your username is "root", error message username is "ODBC" Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348338 Share on other sites More sharing options...
papaface Posted September 14, 2007 Author Share Posted September 14, 2007 Yeah, well connection.php receives no errors and I can perform queries outside the class. Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348345 Share on other sites More sharing options...
papaface Posted September 14, 2007 Author Share Posted September 14, 2007 So I take it no one can help me? :'( Quote Link to comment https://forums.phpfreaks.com/topic/69256-solved-using-other-classes-in-a-class/#findComment-348428 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.