Current location: Hot Scripts Forums » Other Discussions » Database » Database Name issue


Database Name issue

Reply
  #1 (permalink)  
Old 10-26-09, 11:12 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
Database Name issue

Hi there,

is it possible to get the name of the database from the data?
I search my DB with full-text-search..

now I want to link from the results, but I need the name of the
table the result was found in. I use that back in the URL to link..

F.E.

search for: "Example Search" in my DB
In table 1 this is found, I need the name of table 1 to use..

Hope this comes a bit clear to someone...

_j
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-26-09, 11:15 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Can you post your code?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-28-09, 05:57 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
Sure.. I use the following script to search:

PHP Code:

$search_result_algemeen mysql_query("SELECT * FROM mc_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST($word IN BOOLEAN MODE) 
                                     UNION SELECT * FROM stp_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST(
$word IN BOOLEAN MODE)
                                     UNION SELECT * FROM nl_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST(
$word IN BOOLEAN MODE)
                                     UNION SELECT * FROM vb_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST(
$word IN BOOLEAN MODE)
                                     "
) or die('Error, query failed1'); 
Now I need to know from wich table the result comes from so I can link to (f.e.) mc_algemeen.php to show the entire story.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-28-09, 08:19 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
As far as I know there wouldn't be any way to determine which table (or tables) produced the "hit" for a successful search using that query.

Quote:
Originally Posted by jonnekke View Post
Sure.. I use the following script to search:

PHP Code:

$search_result_algemeen mysql_query("SELECT * FROM mc_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST($word IN BOOLEAN MODE) 

                                     UNION SELECT * FROM stp_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST(
$word IN BOOLEAN MODE)
                                     UNION SELECT * FROM nl_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST(
$word IN BOOLEAN MODE)
                                     UNION SELECT * FROM vb_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST(
$word IN BOOLEAN MODE)
                                     "
) or die('Error, query failed1'); 
Now I need to know from wich table the result comes from so I can link to (f.e.) mc_algemeen.php to show the entire story.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-30-09, 03:41 PM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
If you are using MySQL 5 that supports Store Procedures and Functions it would be possible.

You would need to add a function that you can call in the SELECT statement of each union that passed the table name of that union and a row from if there is a value.

This also relies upon MySQL user defined variables.

For example here is a MySQL function that would store the tblName if there are results in inp.

Code:
delimiter //
CREATE FUNCTION `testTableName`(inp VARCHAR(100), tbl VARCHAR(100)) RETURNS text CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
  DECLARE bCheck INT;

  IF inp <> "" THEN
     SELECT @tblName LIKE CONCAT("%",tbl,"%") INTO bCheck;
     IF bCheck <> 1 THEN
        IF @tblName = "" THEN
           SET @tblName = tbl;
        ELSE 
           SET @tblName = CONCAT(@tblName, ",", tbl);
        END IF;
     END IF;
  END IF;  
  RETURN inp;
END
 
//
delimiter ;
So using your code

Code:
SELECT *, testTableName(id, mc_algemeen) FROM mc_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST($word IN BOOLEAN MODE) 
                                     UNION SELECT *,  testTableName(id, stp_algemeen) FROM stp_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST($word IN BOOLEAN MODE)
                                     UNION SELECT *,  testTableName(id, nl_algemeen) FROM nl_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST($word IN BOOLEAN MODE)
                                     UNION SELECT *,  testTableName(id, vb_algemeen) FROM vb_algemeen WHERE MATCH(verhaal, titel, subtitel) AGAINST($word IN BOOLEAN MODE)
After calling the query you can do

Code:
SELECT @tblName;
To get a comma separated list of tables that had results. Just make sure you clear out your user defined value before each run.

Thre is probably a more elegant solution using Stored Procedures however there are some special client flags you must set depending on how you build your stored proc.

Was an interesting problem to tackle.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Database Issue WillUK Database 1 05-07-09 03:58 PM
Filling out a registry form multiple times from an MySQL Database aeisecurity PHP 7 03-25-08 09:09 AM
PHP/Mysql Database issue PHP-nuke renob PHP 2 10-31-05 02:01 PM
Simple, searchable book database max fischer Script Requests 0 05-14-05 02:00 PM


All times are GMT -5. The time now is 01:01 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.