简体   繁体   English

mysql_query()奇怪的行为

[英]mysql_query() Strange behaviour

I have a piece of PHP code which calls to mysql_query() to insert a row in a remote database. 我有一段PHP代码,该代码调用mysql_query()在远程数据库中插入一行。

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. 奇怪的是mysql_query()总是返回false,但是当我检查数据库时,我可以看到该行已正确插入。

My PHP version is 5.4.3 and this is the code I´m using: 我的PHP版本是5.4.3,这是我正在使用的代码:

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. 当然,它将返回false。

Try to use 尝试使用

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

instead of 代替

mysql_close($con);

in your class. 在你班上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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