robgolding63 Posted May 20, 2007 Share Posted May 20, 2007 Hi, I want to start using mysqli on my site instead of mysql, to take advantage of prepared statements. In my mysql.php file, which holds the connect functions for mysql, I added: $mysqli = new mysqli("localhost", "user", "pass", "db"); so now there is a class called mysqli, that I use to carry out DB operations (I hope that's how it works anyway). But when I am inside one of my own classes, I cannot access $mysqli to do queries and such like. I get this error: Fatal error: Call to a member function query() on a non-object in C:\Inetpub\wwwroot\maxms.net\common.php on line 36 What simple thing have I missed? Thanks for any help Rob Quote Link to comment https://forums.phpfreaks.com/topic/52233-mysqli-within-a-class/ Share on other sites More sharing options...
radar Posted May 20, 2007 Share Posted May 20, 2007 Here is how I setup my class -- im not giving a lot of code, just the basics of how i setup my class for my site... <?php class TURBO extends Templatized{ var $db = null; var $st = null; var $adv_stats = null; var $timeoutSeconds = 120; var $numberOfUsers = 0; function TURBO() { $this->template_dir = $_SERVER['DOCUMENT_ROOT'] . '/cesite/templates'; $this->compile_dir = $_SERVER['DOCUMENT_ROOT'] . '/cesite/templates_c'; $this->config_dir = $_SERVER['DOCUMENT_ROOT'] . '/cesite/configs'; $this->force_compile = TRUE; $this->sql_db('localhost', 'user', 'pass', 'database'); } all of my functions goes above the } such as my function TURBO... } ?> then in my index.php i have this code to call it. <?php require_once('../includes/turbo.php'); $turbo =& new TURBO; ?> then in the rest of my index.php i can use $turbo->blah_blah(); to call my functions... Quote Link to comment https://forums.phpfreaks.com/topic/52233-mysqli-within-a-class/#findComment-257699 Share on other sites More sharing options...
robgolding63 Posted May 20, 2007 Author Share Posted May 20, 2007 Well I am wondering how other people have accessed the mysqli class from within other classes (it works if I do it from outside another class). Thanks, Rob Quote Link to comment https://forums.phpfreaks.com/topic/52233-mysqli-within-a-class/#findComment-257721 Share on other sites More sharing options...
trq Posted May 20, 2007 Share Posted May 20, 2007 Well I am wondering how other people have accessed the mysqli class from within other classes (it works if I do it from outside another class). You either need to pass the mysqli object into your class via the construct, or create a new mysqli object within the class. Quote Link to comment https://forums.phpfreaks.com/topic/52233-mysqli-within-a-class/#findComment-257729 Share on other sites More sharing options...
robgolding63 Posted May 20, 2007 Author Share Posted May 20, 2007 OK so would making a new object basically connect again if I did that inside the class? If so, how do I pass the object via the construct? Rob Quote Link to comment https://forums.phpfreaks.com/topic/52233-mysqli-within-a-class/#findComment-257731 Share on other sites More sharing options...
trq Posted May 20, 2007 Share Posted May 20, 2007 how do I pass the object via the construct? In php5. <?php class foo { private $db; function __construct($db) { $this->db = $db; } } $mysqli = new mysqli(); $foo = new foo($mysqli); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52233-mysqli-within-a-class/#findComment-257739 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.