Error to know who is connected as FaceBook

0

Hello friends I have a very strange error with my code to see who is connected but this works when you want there is one that comes online and there are others offline and the weird thing is that there are others that do not even leave anything because that happens a help to be able see who is connected and who is not equal to facebook in the friends menu

session_start();
$userID = '9'; //user
$online = 'yes';
$time_check = $online-10; //We Have Set Time 1 Minutes


$sql="SELECT * FROM user WHERE userID='$userID'"; $result=mysql_query($sql);
$count=mysql_num_rows($result); 

//If count is 0 , then enter the values
if($count=="no"){ 
    $sql="INSERT INTO user(userID, online)VALUES('$userID', '$online')"; 
    $result=mysql_query($sql);
}

// else update the values 
else {
    $sql="UPDATE user SET online='$online' WHERE userID = '$userID'"; 
    $result=mysql_query($sql); 
}
    
asked by Shareiv 07.03.2017 в 02:48
source

1 answer

1

Your way of declaring PHP connection with MySQL is no longer accepted by many of the servers. Look at the PHP version you are using on your local server (wamp / xamp ..). I use PHP 5.6 or PHP 7 and if I write connections as you do: mysql_query , mysql_num_rows would give me error and if I ignored it, the application would not work.

If you are going to publish your website, I strongly suggest you change your code using Mysqli or PDO. I have changed it to PDO. I have not tested it, so it may give some error:

session_start();
$userID = '9'; //user
$online = 'yes';
$time_check = $online-10; //We Have Set Time 1 Minutes

$sql=""; $result=mysql_query($sql);
$count=mysql_num_rows($result); 

$count = $pdo->query("SELECT * FROM user WHERE userID = $userID")->fetchColumn();

//If count is 0 , then enter the values
// !!!!! MIRA A VER SI EL COUNT TIENE QUE DECIR NO O TIENE QUE DECIR 0 PORQUE TU COMENTARIO DICE UNA COSA Y PONES OTRA
if($count=="no") { 
    $stmt = $pdo->prepare("INSERT INTO user(userID, online) VALUES ($userID, $online)");
    $stmt->execute();
}

// else update the values 
else {
    $stmt = $pdo->prepare("UPDATE user SET online = $online WHERE userID = $userID");
    $stmt->execute();
}

BUT FIRST make a connection with PDO:

$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
    
answered by 09.03.2017 в 17:51