Jump to content

Php question


Danny620

Recommended Posts

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(){

	}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/197403-php-question/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/197403-php-question/#findComment-1036137
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.