ianhaney Posted December 11, 2015 Share Posted December 11, 2015 Hi I need bit of help if ok with a php mysqli stats coding I have put in my includes/header.php file as want it on every page but for some reason it is displaying 0 users online, the data is being added to the database but not being retrieved for some reason as thought it would say 1 user online as I am on the site and refreshing it, below is the coding I have on usersonline.php file <?php //Put your basic server info here $server = "localhost"; //normally localhost $db_user = ""; //your MySQL database username $db_pass = ""; //your MySQL database password $database = ""; $timeoutseconds = 300; //this is where PHP gets the time $timestamp = time(); $timeout = $timestamp - $timeoutseconds; //connect to database //$server = localhost probably //$db_user = your MySQL database username //$db_pass = //your MySQL database password $mysqli = new mysqli($server, $db_user, $db_pass, $database); //insert the values $insert = "INSERT INTO useronline VALUES (?, ?, ?)"; $stmt = $mysqli->prepare( $insert ); $stmt->bind_param( 'iss', $timestamp, $_SERVER['REMOTE_ADDR'],$_SERVER['PHP_SELF'] ); if(!$stmt->execute()) { print "Useronline Insert Failed > "; } //delete values when they leave $delete = "DELETE FROM useronline WHERE timestamp < ?"; $stmt = $mysqli->prepare( $delete ); $stmt->bind_param( 'i', $timeout ); if(!$stmt->execute()) { print "Useronline Delete Failed > "; } //grab the results $result = "SELECT DISTINCT ip FROM useronline WHERE file = ?"; $stmt = $mysqli->prepare( $result ); $stmt->bind_param( 's', $_SERVER['PHP_SELF'] ); if(!$stmt->execute()) { print "Useronline Select Error > "; } //number of rows = the number of people online $user = $stmt->num_rows; //spit out the results $mysqli->close(); if($user == 1) { print("$user user online\n"); } else { print("$user users online\n"); } ?> then in my header.php file I have the following <?php require_once("usersonline.php"); ?> Sorry I have tried to work the issue out but I am not getting any errors so bit stuck on what to fix? Quote Link to comment https://forums.phpfreaks.com/topic/299703-php-mysqli-stats-issue/ Share on other sites More sharing options...
mac_gyver Posted December 11, 2015 Share Posted December 11, 2015 you cannot get a result from $stmt->num_rows;, unless you have called $stmt->store_result(); (see the $stmt->num_rows documentation.) this is one more reason to not use mysqli. if you use PDO, all your database code will be simpler. Quote Link to comment https://forums.phpfreaks.com/topic/299703-php-mysqli-stats-issue/#findComment-1527822 Share on other sites More sharing options...
ianhaney Posted December 11, 2015 Author Share Posted December 11, 2015 I think I will look or try to build one using PDO rather than mysqli Quote Link to comment https://forums.phpfreaks.com/topic/299703-php-mysqli-stats-issue/#findComment-1527825 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.