Error updating the php5 to php7 script

0

Sorry for the inconvenience my dear and wise programmers well I have a small error when updating my scripts from php 5 to php 7.

I was updating a small visitor counter and it gives me a lot of errors in some lines to see if you can help me thanks.

online.php

<?php

require_once('conn.php');

$time = 5 ;

$date = time() ;

$ip = $_SERVER['REMOTE_ADDR'];

$limite = $date-$time*60 ;

mysql_query("delete from online where date < $limite") ;

$resp = mysql_query("select * from online where ip='$ip'") ;

if(mysql_num_rows($resp) != 0) {
mysql_query("update online set date='$date' where ip='$ip'") ;
}

else {
mysql_query("insert into online (date,ip) values ('$date','$ip')") ;
}

$query = "SELECT * FROM online";

$resp = @mysql_query($query) or die(mysql_error());

$usuarios = mysql_num_rows($resp);

if($usuarios > 1 || $usuarios == 0){
    echo("Hay ");
    }else{
    echo("Hay ");
}if($usuarios == 0){
    echo("no ");
    }else{
    echo($usuarios." ");
}if($usuarios > 1 || $usuarios == 0){
    echo("Usuarios en línea.");
    }else{echo("usuario en línea.");
}?>

Visitas.php

<?php

include('conn.php');


$ip = $_SERVER['REMOTE_ADDR'];



$sql="select ip, TIMEDIFF(NOW(), fecha), fecha, num_visitas from visitas where ip='$ip'";



$rs=mysql_query($sql) or die("Problemas al ejecutar select SQL ".mysql_error());



$fila=mysql_fetch_array($rs);

$tiempo=$fila[1]; 

$num_visitas=$fila[3]; 

$horas_t=substr($tiempo,0,2); 

$tiemRes = 5; 

if (mysql_num_rows($rs)==0)

{

$sql="insert into visitas(ip, num_visitas, fecha) values('$ip', 1, NOW())";

mysql_query($sql) or die("Problemas al ejecutar la insert SQL ".mysql_error());

}



elseif (mysql_num_rows($rs) > 0 && $horas_t > $tiemRes)

{

$sql="update visitas set fecha=NOW(), num_visitas='$num_visitas'+1 where ip='$ip'";

mysql_query($sql) or die("Problemas al ejecutar update SQL ".mysql_error());

}

$sql="select SUM(num_visitas) from visitas"; 

$rs=mysql_query($sql) or die("Problemas al ejecutar select SQL ".mysql_error());

$fila=mysql_fetch_array($rs); 

$num_visitas=$fila[0]; 

mysql_close($conection); ?>
    
asked by Alex 26.02.2018 в 02:19
source

1 answer

0

It is likely that your functions of connection to the DB and querys execution were removed in php7, which leaves way to use mysqli or PDO .

In this link of the function mysql_connect , it indicates that it was marked as deprecated from php 5.5.0, and removed in php 7.0.0

link

Your flow should be as follows

...
// creamos un objeto de conexión a la base de datos
$conn = new mysqli('localhost','usuario', 'password', 'database');
// generamos una sentencia preparada para ejecutar nuestra insercción de datos
$stament = $conn->prepare("INSERT INTO mitabla(?,?,?)");
// indicamos parametros a utilizar
// s - indica que la variable sera un string
// i - indica que la variable sera un integer
$stament->bind_param("ssi", $campo1, $campo2, $campo3);
// asignamos los valores a las variables, la forma de hacerlo de bind, es que toma las referencias de las variables
$campo1 = "VALOR";
$campo2 = "DESCRIPCIÓN";
$campo3 = 500;
// ejecutamos nuestra sentencia preparada
$statement->execute();
// verificamos si hubo filas afectadas
if($statement->affected_rows > 0)
    echo "El campo fue insertado";
// cerramos nuestra sentencia
$statement->close();
//cerramos nuestra conexión
$conn->close();

for more info in the documentation

link

link

link

    
answered by 26.02.2018 в 02:29