-1

I'm having issues with the mysql_query function in php, the exact problem its within this code

function cierraSesion($cookie) {
    $fecha = date("Y-m-d");
    $hora = date("H:i:s");
    global $connection;
    $query = "UPDATE conection SET FEC_DESCONEXION = '$fecha', FEC_HORADESCON = '$hora' WHERE VAR_COOKIE = '$cookie'";
    mysql_query($query, $connection) or die("error:" . mysql_error());
    }

when i execute this code i get this error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in

Now, you can think its a problem with the "$connection" variable but i use the exact same variable in the other 60 functions in that same file without any problem, i've checked the query making an output with die($query) and execute it manually to check any query mistake, but works fine.

Here is an example of a working function:

function abrirSesion($nombre, $sesion) {
    $fecha = date("Y-m-d");
    $hora = date("H:i:s");
    $fechaf = date("Y-m-d", strtotime("+30 minutes"));
    $horaf = date("H:i:s", strtotime("+30 minutes"));
    global $connection;
    $ip = get_client_ip();
    $query = "INSERT INTO conection (VAR_IDUSUARIO, FEC_CONEXION, FEC_HORACONEX, FEC_DESCONEXION, FEC_HORADESCON, VAR_IP, VAR_COOKIE)
                  VALUES ('$nombre', '$fecha', '$hora', '$fechaf', '$horaf', '$ip', '$sesion');";
    mysql_query($query, $connection) or die("error:" . mysql_error());
}

Thanks for any help you can give me.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
nastrand
  • 131
  • 1
  • 14
  • you should consider switching to mysqli or PDO. – Hristo Valkanov Jun 08 '14 at 23:30
  • I work with a team, and for now they dont want to switch, no matter the function is deprecated, maybe they'll change his opinion when php team remove it. – nastrand Jun 08 '14 at 23:32
  • _you can think its a problem with the "$connection" variable_ - I would think that since that is precisely what the message is telling you. Nevermind that you've used it elsewhere, that's where your problem is here. Is it initialised before you make the call to **this** function? –  Jun 08 '14 at 23:34
  • have you checked if your query executes outside of the PHP code? Also my condolences on the team thing... I know how it feels... – Hristo Valkanov Jun 08 '14 at 23:36
  • Sorry for the fail answer, I thought you only had $connection declared global in the working example.. :X – Xatenev Jun 08 '14 at 23:36
  • @HristoValkanov I've runned the query outside the php using a die($query) and executes correctly. – nastrand Jun 08 '14 at 23:39
  • @MikeW Thanks! after an hour checking i've never checked in the file where i call the function connection isn't defined.. thank for you help, now works :D – nastrand Jun 08 '14 at 23:42

1 Answers1

0

In your function, $connection is outside the scope of your function. So even if it's already been set somewhere else, your function won't see it. You could globalize it, but that's a horrible way to code because it complicated figuring out what variable you're actually referencing (all it takes is one person redefining $connection to break your function).

A best practice here is to inject the variable as a passed parameter

function cierraSesion($cookie, $connection) {
    /** code removed to highlight solution */
    mysql_query($query, $connection) or die("error:" . mysql_error());
}

This way, you're not dealing with guessing about global variables and such.

And I assume you know mysql_query is deprecated so I'll just add a subtle reminder.

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100