Danny620 Posted April 2, 2010 Share Posted April 2, 2010 i have a piece of code here i just wanted to know how to pass mysqli object in to each of these fucntion beacuse i dont want to put require in each function thank you. <?php class Tracker { var $ip_address; var $time; require 'mysqli.php'; function Logger($ip_address){ $ip = $mysqli->real_escape_string($ip_address); //Time NOW $time = time(); $site_ticket = $time + 1800; //Check whether or not the user has been on the site before. $query = "SELECT ip, vists, time, site_ticket FROM tracker WHERE ip='$ip'"; $result = $mysqli->query($query) or die(mysqli_error($mysqli)); if($mysqli->affected_rows == 1){ $row = $result->fetch_object(); //New site ticket. if($time >= $row->site_ticket){ $vist = $row->vists ++; $query = "UPDATE tracker SET vists='$vist', time='$time', site_ticket='$site_ticket' WHERE ip='$ip'"; $result = $mysqli->query($query) or die(mysqli_error($mysqli)); } }else{ //New user to the site. $query = "INSERT INTO tracker (ip, vists, time, site_ticket) VALUES ('$ip', '1', '$time', '$site_ticket')"; $result = $mysqli->query($query) or die(mysqli_error($mysqli)); } } } class Tracking_results { function Today(){ } function Now(){ } function Week(){ } function Most_viewer(){ } } ?> Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted April 2, 2010 Share Posted April 2, 2010 What code is in mysqli.php? Quote Link to comment Share on other sites More sharing options...
Danny620 Posted April 2, 2010 Author Share Posted April 2, 2010 the database settings and new the code to connect to the db Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted April 2, 2010 Share Posted April 2, 2010 Using require is essentially copying and pasting the contents of a file to where the require statement is. The difference between require and include is that a require will cause the script to fail if it can't access the file, an include will not (although it may still throw an error). In any event, if you need access to the variable that lets you connect to the database, you don't need to include the entire database connection script, you just need to define the database connection variable outside of any blocks (so it's not scoped to a function, class, or any other block structure) and then include it in any blocked structures (like your Tracker methods) using the key word global and then the variable name. For example: // some php file requested by the user, say, index.php: require('database.php'); $db = new database_connection(); require('tracker.php'); // tracker.php: class Tracker { var $ip_address; var $time; function Logger($ip_address) { global $db; // etc. } function another_function() { global $db; // function stuff here } // etc. Quote Link to comment 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.