HighDots Forums  

Searching MySQL table names

Macromedia Dreamweaver Macromedia Dreamweaver Discussions (macromedia.dreamweaver)


Discuss Searching MySQL table names in the Macromedia Dreamweaver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ArizonaJohn
 
Posts: n/a

Default Searching MySQL table names - 02-03-2009 , 12:55 AM






Hello,

I would like to search all of the table names in a MySQL
database using a simple HTML form and PHP. I am stuck. I have attached the
code I have so far. How do I apply the search to just the table names in the
database?

Thanks in advance,

John

<form name="search" method="post" action="<?=$PHP_SELF?>">
<input type="text" name="find" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
mysql_connect("hostaddress", "username", "password") or die(mysql_error());
mysql_select_db("sand2") or die(mysql_error());

// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM sand2 WHERE upper($field) LIKE'%$find%'");


Reply With Quote
  #2  
Old   
bregent
 
Posts: n/a

Default Re: Searching MySQL table names - 02-03-2009 , 01:13 AM






I don't use MySQL, but I believe that table names are stored in the 'Tables'
table in the INFORMATION_SCHEMA. So,

SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'sand2'
AND table_name LIKE '%$find%'





Reply With Quote
  #3  
Old   
Gary White
 
Posts: n/a

Default Re: Searching MySQL table names - 02-03-2009 , 09:31 AM



On Tue, 3 Feb 2009 06:13:29 +0000 (UTC), "bregent"
<webforumsuser (AT) macromedia (DOT) com> wrote:

Quote:
I don't use MySQL, but I believe that table names are stored in the 'Tables'
table in the INFORMATION_SCHEMA. So,

SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'sand2'
AND table_name LIKE '%$find%'
A query on the database will get the table names:

myqsl_query("SHOW TABLES FROM sand2 LIKE '%$find%'");

Gary


Reply With Quote
  #4  
Old   
ArizonaJohn
 
Posts: n/a

Default Re: Searching MySQL table names - 02-06-2009 , 11:50 PM



Gary,

Thanks, that seems to work. However, now I have another problem: printing out the entire array of the table that results from the search. Any ideas on how to do that?

Thanks,

John

Reply With Quote
  #5  
Old   
Gary White
 
Posts: n/a

Default Re: Searching MySQL table names - 02-07-2009 , 06:39 AM



On Sat, 7 Feb 2009 04:50:17 +0000 (UTC), "ArizonaJohn"
<webforumsuser (AT) macromedia (DOT) com> wrote:

Quote:
Gary,

Thanks, that seems to work. However, now I have another problem:
printing out the entire array of the table that results from the search.
Any ideas on how to do that?
You're welcome, John. What problem are you having? It should be very
straightforward.

$result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'")
or die(mysql_error());
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_row($result)){
print "$row[0]<br />";
}
}else{
print "None found";
}

Gary


Reply With Quote
  #6  
Old   
ArizonaJohn
 
Posts: n/a

Default Re: Searching MySQL table names - 02-07-2009 , 06:40 PM



Hi Gary,

I tried the loop you provided, and when I perform the search, it is only
displaying the name of the table. I would like to display the rows within the
table. Normally, I would expect a while loop to get the job done, but for some
reason it is not working for me. Am I missing something?

Thanks,

John


Reply With Quote
  #7  
Old   
Gary White
 
Posts: n/a

Default Re: Searching MySQL table names - 02-08-2009 , 06:21 AM



On Sat, 7 Feb 2009 23:40:04 +0000 (UTC), "ArizonaJohn"
<webforumsuser (AT) macromedia (DOT) com> wrote:

Quote:
I tried the loop you provided, and when I perform the search, it is only
displaying the name of the table. I would like to display the rows within the
table. Normally, I would expect a while loop to get the job done, but for some
reason it is not working for me. Am I missing something?
The SQL that you're using only returns the names of tables in the database
containing the value in the $find variable. If your goal was to search in
the tables for some data, you're on the wrong track.

Gary


Reply With Quote
  #8  
Old   
ArizonaJohn
 
Posts: n/a

Default Re: Searching MySQL table names - 02-09-2009 , 02:22 PM



Hi Gary,

My goal is to search the table names, but then print the contents of the table that matches the search.

Any ideas on how I can do this?

Thanks,

John

Reply With Quote
  #9  
Old   
Gary White
 
Posts: n/a

Default Re: Searching MySQL table names - 02-09-2009 , 07:10 PM



On Mon, 9 Feb 2009 19:22:47 +0000 (UTC), "ArizonaJohn"
<webforumsuser (AT) macromedia (DOT) com> wrote:

Quote:
My goal is to search the table names, but then print the contents of the
table that matches the search.

Any ideas on how I can do this?

Thanks,

You're welcome, John. Untested code, so beware of typos:

$result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'")
or die(mysql_error());
if(mysql_num_rows($result)>0){
while($table=mysql_fetch_row($result)){
print "<p>Table: $table[0]</p>\n";
$r=mysql_query("SELECT * FROM `$table[0]`");
while($row=mysql_fetch_row($r)){
print "<tr>"
foreach($row as $field){
print "<td>$field</td>";
}
print "</tr>\n";
}
print "</table>\n";
}
}else{
print "None found";
}

Gary


Reply With Quote
  #10  
Old   
ArizonaJohn
 
Posts: n/a

Default Re: Searching MySQL table names - 02-11-2009 , 08:51 PM



Hi Gary,

Thanks for the help. When I try the code you suggest, it gives me this error:

Parse error: syntax error, unexpected T_FOREACH

It looks like the "foreach" loop is not working. Any ideas?

Thanks,

John


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.