0

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

Hello everyone and good day to all

I have a very strange error this morning and iv been trying to solve it for the past 4 hours with no success.

After executing the below code i get this error:

mysql_num_rows() expects parameter 1 to be resource, boolean given in

$per_page = 4;
    require_once('Connections/webiceberg.php');
    $sql = "select * from services" ;
    $rsd = mysql_query($sql);
    $count = mysql_num_rows($rsd);
    $pages = ceil($count/$per_page);

But then when i edit this line of code to give me an error i get No Database Selected.

$count = mysql_num_rows($rsd) or die (mysql_error());

Whats up , how can i fix this, why is this happening, its like it cant connect to the db even when on another page iam viewing records from the same table just fine. Any help will be most appreciated.

Community
  • 1
  • 1
Gunnit
  • 1,064
  • 5
  • 22
  • 45

3 Answers3

2

The error is because the mysql_query returns false - meaning there was something wrong with the query.

You may not have executed a use db statement before the select query.

Jan S
  • 1,831
  • 15
  • 21
1

I think that's incorrect useage of mysql_query.

It requires the database to be specified to - so it should be something like this:

$database = mysql_connect($host, $user, $password);
$rsd = mysql_query($sql, $database);

Because of this, $rsd is false when you run mysql_num_rows(), hence the warning about the boolean.

Hope this helps

EDIT: useage isn't techincally incorrect, as the link_identifier parameter is optional, however as detailed here: http://php.net/manual/en/function.mysql-query.php , if you don't specify the database connection it will try to guess and / or connect on its own which is probably not working, hence the error.

totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
  • i added this to the code --->mysql_select_db($database_webiceberg, $webiceberg); and it works , thanks for the inspiration and help everybody The only strange thing that i dont understand is that yesterday it was working without this line of code and today i wasted 4 hours to figure out the error Strange ........ – Gunnit Nov 16 '11 at 11:36
  • perhaps your connection file changed? lol should have spotted that myself, it's tripped me up a few times :) – totallyNotLizards Nov 16 '11 at 11:58
0

You Need to select the database. I think your missing the mysql_select_db function. The below should isolate the error.

    $conn_id = mysql_connect( $host, $username, $password );
    if( !$conn_id ) {
        die('Cannot connect to DB');
    }

     $db = mysql_select_database( $database );

     if( !$db ){
         die('Cannot select the database');
     }

     $sql = "SELECT * from services";
     $result = mysql_query( $sql ) or die(mysql_error());
Eoin Murphy
  • 813
  • 6
  • 9