-1

I'm encountering a very strange problem suddenly.

With a single MySql connection file I get no error. but when i use two or more connections files like,

<?php 
require_once('Connections/a.php'); 
require_once('Connections/b.php'); 
?>

I get a warning something like

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/user/public_html/mydirectory/test.php on line 150

in the following example code. I have indicated where line number 150 is the the following code.

$maxRows_test = 5;
$pageNum_test = 0;
if (isset($_GET['pageNum_test'])) {
  $pageNum_test = $_GET['pageNum_test'];
}
$startRow_test = $pageNum_test * $maxRows_test;

mysql_select_db($database_y, $y);
$query_test = "SELECT mytable.id, mytable.title FROM mytable WHERE mytable.id >=1";
$query_limit_test = sprintf("%s LIMIT %d, %d", $query_test, $startRow_test, $maxRows_test);
$test = mysql_query($query_limit_test, $y) or die(mysql_error());
$row_test = mysql_fetch_assoc($test);

if (isset($_GET['totalRows_test'])) {
  $totalRows_test = $_GET['totalRows_test'];
} else {
  $all_test = mysql_query($query_test);
  $totalRows_test = mysql_num_rows($all_test); //////////---->>>>>> Line 150
}
$totalPages_test = ceil($totalRows_test/$maxRows_test)-1;

I can't find out any logic behind such warning. The connection file contains typical mysql connection code like

$hostname_y = "localhost";
$database_y = "abc";
$username_y = "cde";
$password_y = "efg";
$y = mysql_pconnect($hostname_y, $username_y, $password_y) or trigger_error(mysql_error(),E_USER_ERROR); 

I went through many similar topics in here but I can't relate the warning with connection files somehow in any of the many topics. This is really very strange.

Any idea?

Klanto Aguntuk
  • 717
  • 1
  • 17
  • 42
  • Are you including the same connection file twice, or is that just an example? – andrewsi Apr 26 '14 at 16:01
  • @andrewsi i don't know who did that but there are two separate connections files there. i edited my post. – Klanto Aguntuk Apr 26 '14 at 16:05
  • @John Conde , i had no other choice. if you read my question carefully from top to bottom you of course realize that title may be duplicate but the reason of the question is unique and very strange. – Klanto Aguntuk Apr 26 '14 at 16:08
  • 1
    @SilentPond - that makes it clearer. If you don't add an explicit database connection to a mysql_ call, it will use the most recent connection as a default. What I expect is happening is that the query that is failing needs to use the connection from the first file, and it's falling over because it's actually running on the connection from the second file. All you need to do is add the second parameter to the query function call, and pass in the right database connection. – andrewsi Apr 26 '14 at 16:08
  • @ andrewsi that's not it. the code which is resulting the error use first connection file. – Klanto Aguntuk Apr 26 '14 at 16:13
  • 1
    @SilentPond - right. The default connection used by queries is the one from the _second_ connection - it defaults to the most recently opened connection. So anything that needs to use the connection in the first file needs to be told explicitly to use that connection instead of the default one. – andrewsi Apr 26 '14 at 16:17
  • @andrewsi how should i do that? what's the code? do i only change the ascending order of connection files? – Klanto Aguntuk Apr 26 '14 at 16:23
  • 1
    @SilentPond - in your example code, the database connection is `$y`. If you call `mysql_query($sql, $y)`, that will explicitly use that connection for that query. – andrewsi Apr 26 '14 at 16:25
  • @andrewsi isn't it already there? `$test = mysql_query($query_limit_test, $y) or die(mysql_error());` do you clarify? – Klanto Aguntuk Apr 26 '14 at 16:38
  • @SilentPond - it's missing from the call on line 149, and it looks like that would be the call giving you the error – andrewsi Apr 26 '14 at 16:39
  • yes @andrewsi you are right. it's been solved soon after i changed the code to `$all_test = mysql_query($query_test, $y);` thanks. i don't know if it does make any sense at all as the relevant connection was called explicitly in the main query. – Klanto Aguntuk Apr 26 '14 at 16:55
  • 1
    @SilentPond - you need to pass in the connection every time you're not relying on the default one - if you removed it from the earlier call, then that one would fail instead. The default connection is the most recently opened, not the most recently used. When I've had to do something like this, I always pass the connection in, just so it's perfectly clear which call is using which connection. – andrewsi Apr 26 '14 at 17:05
  • @andrewsi , thanks again for small but important & useful tip. – Klanto Aguntuk Apr 26 '14 at 17:31

1 Answers1

1

This means that mysql_query() has returned false, i.e. it has failed. Check for an error when querying.

MrZebra
  • 11,317
  • 7
  • 36
  • 48