1

im sending data from my android application to mySQL in localhost and I receive warning on (Warning: mysql_free_result() expects parameter 1 to be resource, Boolean given in C:\xampp\htdocs\datatest.php on line 17)

despite the warning i'm still able insert the data into the database.

i'm wondering is it ok to ignore this or how can i solve this problem?

i tried various forums and website by none solve my problems.

<?php 
$dbcnx = mysql_connect("localhost", "root", "");
$db = "agentdatabase";
mysql_select_db($db, $dbcnx);
$user_id=$_POST['username'];
$passwd=$_POST['password'];
$query = "INSERT INTO agentable (username,password) VALUES ('".$user_id."','".$passwd."')";
echo $query;

$result = mysql_query($query) or die ("<b>Query failed:</b> " . mysql_error());

if($result){
    echo '<br />','pass';
}
else echo mysql_error();

mysql_free_result($result);
mysql_close($dbcnx);
?>
Daryl
  • 95
  • 1
  • 2
  • 10
  • Possible duplicate [mysql_fetch_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select?answertab=votes#tab-top) – John Conde Jan 25 '13 at 18:33
  • That's... not a duplicate. – Ignacio Vazquez-Abrams Jan 25 '13 at 19:47

5 Answers5

11

You can't free the result of an INSERT query, since you can't free a boolean.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets.

mysql extension is deprecated, instead use the MySQLi or PDO_MySQL.

mysql_query() only returns a resource for SELECT, SHOW, EXPLAIN, and DESCRIBE queries.

rizon
  • 8,127
  • 1
  • 27
  • 17
2

Side note, the MySQL PHP extension is deprecated. It's better to use MySQLi or PDO.

Yo-han
  • 351
  • 2
  • 12
1

Here's another possible reason that I tested and was able to reproduce this error in two other different ways which have not been mentioned here.

1) Your SQL has the wrong syntax and looks like this (notice the incorrect extra comma after field3:

"select field1, field2, field3, from myTable";

2) Your SQL contains a duplicate field and looks like this:

"select field1, field2, field2 from myTable";

In both cases I got the message and it was displayed in different functions:

"Warning: mysql_result() expects parameter 1 to be resource, boolean given"

"Warning: mysql_free_result() expects parameter 1 to be resource, boolean given"

"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given"

Riquet
  • 11
  • 2
0

use this:

if (is_bool($result) === false) {
    mysql_free_result($result);
}
uromay
  • 329
  • 3
  • 11