-1

I need a second pair of eyes on this. I have a database which consists of thousands of tables, all with the same structure. I'm selecting each table then selecting the URL from the rows and checking to see if the URL returns good HTML. The whole thing works fine and it runs through OK once then for subsequent instances of the while function the SQL statement is returning a boolean.

I'm getting this error for all but the first instance, which is running through absolutely fine.

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/toplist/public_html/config/index.php on line 1272

$result = mysql_query("SELECT `access_name` FROM `multiforums_forums`");
while ($names=mysql_fetch_array($result)) {
$acces = $names[access_name];
    $get_urls = mysql_query("SELECT * from `".$acces."_users`");
    while ($results=mysql_fetch_array($get_urls))
            {
              //this bit works fine
            }
}

Hope this makes sense

fedorqui
  • 252,262
  • 96
  • 511
  • 570
  • 4
    That means the query is failing. You need to check for errors and echo `mysql_error()` to see the reason. – Barmar Apr 09 '13 at 21:46
  • 1
    Welcome to StackOverflow! First of all, please don't use mysql_* functions in new code http://bit.ly/phpmsql. They are no longer maintained and are officially deprecated https://wiki.php.net/rfc/mysql_deprecation. Please use PDO http://php.net/pdo or MySQLi http://php.net/mysqli. This article: http://j.mp/QEx8IB will help you decide which. – Revent Apr 09 '13 at 21:46
  • Agree with Barmar. Try `mysql_query("SELECT * from ".$acces."_users") or die(mysql_error());`. – showdev Apr 09 '13 at 21:46
  • @Revent You can't use prepared statements for this purpose. – Barmar Apr 09 '13 at 21:47
  • Good point @Barmar, edit made. Thanks. – Revent Apr 09 '13 at 21:48
  • 2
    @showdev Because you can't use placeholders for the table name, you can only use placeholders in places where a value is allowed. – Barmar Apr 09 '13 at 21:49
  • @Barmar True, thank you. (However, you can still use PDO rather than `mysql_*` statements.) – showdev Apr 09 '13 at 21:49

2 Answers2

2

Read the manual page of mysql_query(). It will return false (boolean) if the query fails. When reading your question, it is likely that this is the case.

You can use the function mysql_error() to get the latest error message from mysql. So change your code to something like this to see the error message:

$result = mysql_query("SELECT `access_name` FROM `multiforums_forums`");
if(!$result) {
    die(mysql_error());
}
while ($names=mysql_fetch_array($result)) {
$acces = $names[access_name];
    $get_urls = mysql_query("SELECT * from `".$acces."_users`");
    if(!$get_urls) {
        die(mysql_error());
    }
    while ($results=mysql_fetch_array($get_urls))
            {
              //this bit works fine
            }
}

Also you should, note that the mysql_* extension for PHP is deprecated. You should not use it for new code if you don't want to rewrite this code soon. Use PDO or the mysqli_* extension instead.

hek2mgl
  • 143,113
  • 25
  • 227
  • 253
0

Maybe you meant

$acces = $names['access_name'];
James
  • 18,446
  • 2
  • 23
  • 39