0

Based on advice that I have been given on StackOverflow, I tried the query below but it did not work. I'm trying to get a list of the 25 most recently-added values for "site" in the database, regardless of what table they are in. The code below give the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in domain.php on line 82

Line 82 has while ($rowa = mysql_fetch_array($indexa))

Any ideas why it's not working?

echo "<table class=\"samples\">";
$index = mysql_query("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='sitefeather'");
while ($row = mysql_fetch_array($index))
{
    $indexa = mysql_query("select site FROM index order by createdatetime desc limit 25");
    while ($rowa = mysql_fetch_array($indexa))
    {    
        echo '<tr><td><a href="sitelookup3.php?entry='.urlencode($rowa['site']).'&searching=yes&search=search">'.$rowa['site'].'</a></td></tr>';
    }    
}
echo "</table>";
3
  • i get an error when i execute "select site FROM index order by createdatetime desc limit 25", it says table index must not be written in this way. And i wonder why do you want to run the same innerquery which has no relevance from outer query. Oct 31, 2009 at 6:14
  • 1
    You are getting this error, because index is a reserved word in mysql, so if you are going to use it as a table name, it must be escaped (in your case, mysql is trying to interpret it as if it was a real keyword).
    – shylent
    Oct 31, 2009 at 6:25
  • FYI: what you're using is called a nested while loop. I'd also suggest you read about code indentation.
    – Artelius
    Oct 31, 2009 at 6:38

5 Answers 5

2

You probably want a variable in there in place of index. Maybe this?

$indexa = mysql_query("select site FROM {$row['TABLE_NAME']} order by createdatetime desc limit 25");

However, um... what are you doing? I don't know what exactly you're trying to accomplish but there are very loud alarms bells going off in my head. Having a dynamic table name in a query is a major red flag and is a sign of poor database design.

My database has a variable number of tables, all with the same structure.

That's bad.

What's in these tables? Let us help you get all this data into one table.

The most straightforward way is to create a single table with an extra column containing the name of the table you're currently storing each row in. Instead of having tables "foo", "bar", and "baz", create a single table with a column containing either "foo", "bar", or "baz" as the string value.

3
  • i too thought of the same, but still it doesn't make sense as i believe he's trying to query some meta table which will have the column createdatetime. Oct 31, 2009 at 6:15
  • Thanks... that still gave me an error. My database has a variable number of tables, all with the same structure. I want to print a list of the 25 most recently added rows from all of the tables total.
    – John
    Oct 31, 2009 at 6:41
  • 1
    "I want to print a list of the 25 most recently added rows from all of the tables total." ...and this is exactly why creating a bunch of tables with the same structure is a BAD idea.
    – longneck
    Oct 31, 2009 at 18:46
1

The query

select site FROM index order by createdatetime desc limit 25

shouldn't work. "index" is a reserved word.

Do you want to use $row['TABLE_NAME'] in there instead?

$indexa = mysql_query("select site FROM " + $row['TABLE_NAME'] + " order by createdatetime desc limit 25");
0

Your innerquery is not relevant with the outer query, you might like to try this

<?php
echo "<table class=\"samples\">";
$index = mysql_query("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='jammulinks'");
while ($row = mysql_fetch_row($index))
{
$indexa = mysql_query("select site FROM $row[0] order by createdatetime desc limit 25");//assuming you have site and createddatetime column there.
while ($rowa = mysql_fetch_array($indexa))
{

  echo '<tr><td><a href="sitelookup3.php?entry='.urlencode($rowa['site']).'&searching=yes&search=search">'.$rowa['site'].'</a></td></tr>';
}

}
echo "</table>";
?>
0

I think what you're trying to do here, is use the first query ($index) to do a SELECT on all the table names returned. In that case, you should be doing something like this:

echo "<table class=\"samples\">";
// Get a list of table names from the schema
$index = mysql_query("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='sitefeather'");
while ($row = mysql_fetch_array($index))
{
    // For every table in the schema, select site from it
    $indexa = mysql_query("select site FROM {$row['table_name']} order by createdatetime desc limit 25");
    while ($rowa = mysql_fetch_array($indexa))
    {
        echo '<tr><td><a href="sitelookup3.php?entry='.urlencode($rowa['site']).'&searching=yes&search=search">'.$rowa['site'].'</a></td></tr>';
    }

}
echo "</table>";

I'm not sure if it will error if the site column does not exist in some of the tables you're querying, but be aware.

0

Consider that you can use reserved words for table names and columns if you escape the token with backticks or as a dereference of a table.

mysql> create table `index` ( id int(11) );
Query OK, 0 rows affected (0.06 sec)

mysql> show tables like 'index';
+----------------------------+
| Tables_in_umbrella (index) |
+----------------------------+
| index                      | 
+----------------------------+
1 row in set (0.00 sec)

mysql> select * from `index`;
Empty set (0.00 sec)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.