Forum Moderators: coopster
database: books
================================================
Title ¦Author¦descript¦shipping
===============================================
since all tables have the title field, i thought i could use that for the mysql query. Also would it be possible to print out the tablename if there is a match?
Anyway, as for you question, it can be done. You could use a first query using
mysql_list_tables() [php.net]
to get all the names, then you could loop through the list and either keep adding an "OR" to the WHERE clause as in
OR $table_list[$i] = $title
or you could send 100 queries, which would make it easy to get the table name back.
Example
============================================
OUTPUT
============================================
Dishwasher(12)
Microwave(3)
Stove(0)
==============================================
<?php
$user="red";
$pw="red";
$dbname = 'AppliancesANDKitchen';
$mysql_access = mysql_connect("localhost", $user, $pw);
$result = mysql_list_tables($dbname);
if (!$result) {
print "DB Error, could not list tables\n";
print 'MySQL Error: ' . mysql_error();
exit;
}
$resultcount=0;
while ($row = mysql_fetch_row($result)) {
$query = "SELECT title FROM $row[0] WHERE title LIKE '%" . $search . "%'";
$list($totalrows)=mysql_fetch_row(mysql_query($query));
if($totalrows=="0") {
$resultcount=0;
}
else{
$resultcount=$resultcount + 1;
}
}
print "$row[0]($resultcount)";
mysql_close($mysql_access);
?>
$list($totalrows)=mysql_fetch_row(mysql_query($query));
First off, I think you mean $list[$totalrows] (that is, it should be an array that is storing the rows, no?)
Second, $totalrows is undefined.
It looks like all you are trying to do based on your sample output is print the names of the tables and the number of hits in each one for the search. In that case,
$result = mysql_query($query);
$list[$row[0]] = mysql_num_rows($result);
Then, once you've fully built up your list
foreach($list as $title => $count)
{
echo "The table $title returned $count hits";
}
Alternatively, if all you're doing is building an output string, you coudl do this in your initial while loop and just save it all to one string there.
<?php
$user="red";
$pw="red";
$dbname = 'AppliancesANDKitchen';
$mysql_access = mysql_connect("localhost", $user, $pw);
$result = mysql_list_tables($dbname);
$search='red';
if (!$result) {
print "DB Error, could not list tables\n";
print 'MySQL Error: ' . mysql_error();
exit;
}
$resultcount=0;
$totalrows=0;
while ($row = mysql_fetch_row($result)) {
$query = "SELECT title FROM $row[0] WHERE title LIKE '%$search%'";
$list[$row[0]] = mysql_num_rows($result);
}
foreach($list as $title => $count)
{
echo "The table $title returned $count hits<br/>";
}
mysql_close($mysql_access);
?>
Anyway, you're counting from your first and only $result and not sending another query. It's telling you the number of tables returned.
You need something like this
while ($row = mysql_fetch_row($result)) {
$hits_query = "SELECT title FROM $row[0] WHERE title LIKE '%$search%'";
$hits_result = mysql_query($hits_query);
$list[$row[0]] = mysql_num_rows($hits_result);
}
SQL SHOW TABLES [FROM db_name] [LIKE 'pattern']statement instead.
Actually, I guess I never knew, since I can't think of an instance where I've used mysql_list_tables(), but I suppose it was deprecated at the same time and for the same reasons as mysql_list_fields() [php.net].
$result=mysql_query("SHOW TABLES FROM $dbname");
to work. For some reason i get the
mysql_num_rows(): supplied argument is not a valid MySQL result resource in...
+++++++++++++++++++++++++++++++++++++++
Non working Versiton Using SHOW TABLES
+++++++++++++++++++++++++++++++++++++++
<?php
$user="red";
$pw="red";
$dbname = 'AppliancesANDKitchen';
$mysql_access = mysql_connect("localhost", $user, $pw);
$result = mysql_query("SHOW TABLES FROM $dbname");
$search='red';
if (!$result) {
print "DB Error, could not list tables\n";
print 'MySQL Error: ' . mysql_error();
exit;
}
$resultcount=0;
$totalrows=0;
while ($row = mysql_fetch_row($result)) {
$hits_query = "SELECT title FROM $row[0] WHERE title LIKE '%$search%'";
$hits_result = mysql_query($hits_query);
$list[$row[0]] = mysql_num_rows($hits_result);
}
foreach($list as $title => $count)
{
echo "The table $title returned $count hits<br/>";
}
mysql_close($mysql_access);
?>
$hits_query = "SELECT title FROM $row[0] WHERE title LIKE '%$search%'";
$hits_result = mysql_query($hits_query)
or exit('Error (' . mysql_errno() . '): ' . mysql_error());
$list[$row[0]] = mysql_num_rows($hits_result);