0

Output:

Warning: mysql_query(): 6 is not a valid MySQL-Link resource in C:...\mysql_helper.php on line 94

$con = mysql_connect($GLOBALS['mysql_host'], $GLOBALS['mysql_username'], $GLOBALS['mysql_password']) or die(mysql_error());
$db = mysql_select_db($GLOBALS['mysql_database']) or die(mysql_error($con));


$username=sanitize_mysql($username);
$password=sanitize_mysql($password);
$email=sanitize_mysql($email);

if(check_exists("users", "username", $username) == FALSE){ 
    $query = "INSERT INTO users VALUES('".$username."','".$password."','".$email."','".$status."','".$reg_date."','".$own_ref_id."','')";
    $result = mysql_query($query,$con) or die(mysql_error($con));
    return TRUE;
} else {
    return FALSE;
}
mysql_close($con);

Works in every other function built like this (copy/paste)

This is check_exists

    function check_exists($table,$specifier,$value)
{
    $con = mysql_connect($GLOBALS['mysql_host'], $GLOBALS['mysql_username'], $GLOBALS['mysql_password']) or die(mysql_error());
    $db = mysql_select_db($GLOBALS['mysql_database']) or die(mysql_error($con));


    $query = "SELECT * FROM ".$table." WHERE ".$specifier." = '".$value."'";
    $erg = mysql_query($query) or die(mysql_error());

    while ($row = mysql_fetch_array($erg)) {
        mysql_close($con);
        return TRUE;
    }
    mysql_close($con);
    return FALSE;
}
Liam Schnell
  • 474
  • 8
  • 25

1 Answers1

4

It appears that the $con in check_exists() is in the same scope as your $con and, therefore, check_exists() first overrides (and loses) your original connection then subsequently closes its own connection when it calls mysql_close($con).

You would be better to maintain a single connection that is left open for the use of all such functions.

eggyal
  • 122,705
  • 18
  • 212
  • 237
  • Even if I change the $con in check_exists to $con2 it won't work. How do you think I can use just one connection? Pass the $con as argument for every function that is using mysql access? – Liam Schnell May 09 '12 at 09:38
  • @LiamSchnell: Either that, or declare `global $con` at the start of each function to access a global variable (if your `mysql_helper` is a class, you could use member variables e.g. `$this->con`). – eggyal May 09 '12 at 09:39
  • Think I am going with this one: [link](http://stackoverflow.com/questions/10208691/do-i-need-a-php-mysql-connection-in-each-function-that-uses-database) – Liam Schnell May 09 '12 at 09:40