1

I'm not very PHP savvy. My current host switched from PHP 5 to 7 and my users online script isn't working. It works via PHP and MySQL to create a table and delete it after 10 minutes.

Can anyone help making this PHP 7 compatible?

This is what the base code is:

CREATE TABLE `user_online` (
`session` char(100) NOT NULL default '',
`time` int(11) NOT NULL default '0'
) ENGINE=MyISAM;

<?php

session_start();
$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="user_online"; // Table name

// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE session='$session'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count=="0"){

$sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
$result1=mysql_query($sql1);
}

else {
"$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
$result2=mysql_query($sql2);
}

$sql3="SELECT * FROM $tbl_name";
$result3=mysql_query($sql3);

$count_user_online=mysql_num_rows($result3);

echo "User online : $count_user_online ";

// if over 10 minute, delete session
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);

// Open multiple browser page for result


// Close connection

mysql_close();
?>

1 Answers1

0

Your code is not working because the mysql extension was removed from PHP 7. It has been deprecated since PHP 5.5 (June 2013!).

It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7.

You can see the options you have in the manual.

You basically have two options, PHP Data Objects (PDO), or MySQL Improved (mysqli). Both support prepared statements, which will allow you to develop virtually SQL injection-free apps.

If you need to keep a procedural style, you are stuck with mysqli, since PDO is object-oriented only.

Other than that, whichever you go with is mostly about choice. In the page I linked from the manual you'll find a very nice comparison table in Feature comparison.

The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%.

ishegg
  • 9,685
  • 3
  • 16
  • 31