开发者

mysql_query returns false after successfully executing query in PHP

I have a strange bug where I call an insert query, it executes successfully (the row appears in the DB) but mysql_query returns false. It only returns false when a particular field contains an underscore or dash (or probably some other chars but these are the two I've run into so far).

"INSERT INTO Hoopoes (name, owner, dbuser, dbpass, package) VALUES "
                        ."('".$this->config['hname']."', '".$this->config['email']."','".$db_prefix.$db_username."','$db_userpass',"
 开发者_C百科                       ."'".$this->config['package']."')"

So this is the query. It fails when config['hname'] contains _ or -. I don't understand why. The row appears in the DB, but mysql_query still returns false. Any insight would be much appreciated


It's good to check what mysql_error() returns. In my case it was 'table database_name.table_name doesn't exist'. That has given me a clue and after second check I realized that i've created table_name in PHPMyAdmin in another database than database_name :P


You mus select the database after connect and before any query:

// Connect...
mysql_connect("server", "user", "pass");
// ...and select the database!
mysql_select_db("database");

// Now you're good to go. :)

You can use your INSERT query without select a database when you specify the schema, like this:

mysql_query("INSERT INTO `database`.`table` VALUES (...);


Hmm.. this was a strange one. The query worked and successfully inserted apparently without a database selected. I added a mysql_select_db call directly above the query, and now it works just the same, but the query returns true.

Strange

Thanks for the help!


Even if the SQL query is successfully executed doesn't means it should return a TRUE. It only returns TRUE if the queries are executed in a proper order. Hope that is why you are getting a FALSE.

http://dev.mysql.com/doc/refman/5.7/en/mysql-real-query.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜