0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

$connection = mysql_connect('localhost', 'username', 'password') or die('Database connection failed:' . mysql_error());                                

mysql_select_db('db_test', $connection) or die('Database connection failed: ' . mysql_error());

$var = '';

switch ($var) {
    case 'dosomething':
        break;
    default:
        default_func();
}

function default_func() {
    if (isset($_POST['submit'])) {
        $query = "INSERT INTO `table_test` (`some_field`) VALUES ('test')";
        $result = mysql_query($query, $connection) or die(mysql_error());

    header('Location: index.php?submit=success');
    }
}

mysql_close($connection);

I have been getting a "mysql_query() expects parameter 2 to be resource null given error".

I made sure that $connection is assigned to the value of the database connection resource id, so that doesn't seem to be the problem. Don't know why the value of $connection is giving me a NULL. Any explanation as to why this error is occurring?

Community
  • 1
  • 1
user1307016
  • 383
  • 1
  • 8
  • 17

5 Answers5

3

$connection variable is not in the scope of default_func() function.

If you have only one connection - just omit the second parameter

zerkms
  • 240,587
  • 65
  • 429
  • 525
3

The $connection variable is not visible within default_func. If you want it to be, just declare it there as global:

function default_func() {
    global $connection;
  ...
}

However, global variables are likely to cause hard-to-diagnose errors when the code gets slightly more complex. Hidden global state (which is what you're using if you leave the second parameter off of mysql_query entirely) is even worse. You're better off passing the connection into the function:

switch ($var) {
    case 'dosomething':
        break;
    default:
        default_func($connection);
}

function default_func($conn) {
    if (isset($_POST['submit'])) {
        $query = "INSERT INTO `table_test` (`some_field`) VALUES ('test')";
        $result = mysql_query($query, $conn) or die(mysql_error());

    header('Location: index.php?submit=success');
    }
}
Mark Reed
  • 86,341
  • 15
  • 131
  • 165
2

$connection is not defined inside the function.

It is not required anyway, since if you just leave it out the last opened connection is assumed.

Solution: Just drop the ,$connection from the mysql_query call.

Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
1

$connection is not defined in that scope, pass connection to the function or declare it as global or just leave it out since you only have one connection
PHP VARIABLE SCOPE

Musa
  • 93,746
  • 17
  • 112
  • 129
1

As you have not decalred $connection variable inside your function, and you want to use the outside variable $connection, you have to use it as global $connection so that the scope of the outside variable persist within you function

Rohit Choudhary
  • 2,205
  • 1
  • 24
  • 34