简体   繁体   中英

mysql_query() Strange behaviour

I have a piece of PHP code which calls to mysql_query() to insert a row in a remote database.

The strange thing is that mysql_query() always returns false but when I check my database I can see that the row was inserted properly.

My PHP version is 5.4.3 and this is the code I´m using:

class dbmanager{

    public function executeQuery($sql){
       $con = mysql_connect(config::getBBDDServer(), config::getBBDDUser(), config::getBBDDPwd());
       if (!$con){
           die('Could not connect: ' . mysql_error());
       }

       mysql_select_db(config::getBBDDName(), $con);

       $result = mysql_query($sql);

       mysql_close($con);

       return $result;
    }
}

// ANOTHER FILE...

private function insertRow($name, $descrip){
    $sql = sprintf("INSERT INTO myTable (name, descripction) VALUES ('%s', '%s')", mysql_real_escape_string($name), mysql_real_escape_string($description));    

    $db = new dbmanager();
    $result = $db->executeQuery($sql);

    if($result){ // always FALSE
        return mysql_insert_id();
    }else{
        return false;
    }
}

Anybody has an idea about what is happening here?

You close connection to DB

mysql_close($con);

after that you try to call

mysql_insert_id();

Of course, it will return false.

Try to use

public function __destruct () {
    mysql_close($con);
}

instead of

mysql_close($con);

in your class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM