Jump to content

MySQLi within a class


robgolding63

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/52233-mysqli-within-a-class/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/52233-mysqli-within-a-class/#findComment-257699
Share on other sites

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.