简体   繁体   中英

Getting the name of a row's table in PHP and MySQL

(I'm not entirely sure how to word this question, which made Googling it a fruitless challenge, so I hope that this question isn't too common or poorly-worded.)

I'm working on a web application in PHP and MySQL that will allow a user to search through a database made up of 12 tables (so every entry on the results page could be from any one of 12 tables). Pretty simple stuff, and the search functionality is already there and working great. Now I want for the user to be able to click an entry in the search results and be taken to a page that is populated with the details for that particular result (very similar to running a search on Stack Overflow, then clicking one of the results to be taken to the full question / answer page for that particular result).

I figure the easiest way to do this would be URL parameters: one for the ID of the result (the row number, basically), and one for the table name, so that on the results page I can just tell it which table and row to reference and then fill in the information from there. I can get the ID just fine (as it's one of the columns in the tables), but I can't figure out how to tell PHP which table the given row is from so that I may pass its name into the URL . (Example: User clicks on a result that is row 4 from a table called "employees". URL parameters would become ?table=employees&id=4 . The "employees" part is where I'm having issues.)

Any help would be greatly appreciated! :) Abridged relevant code below:

[...]

// The 12 tables are split across two languages, so the loop is made of two parts -- an English loop and a Japanese loop. lineToString() is a custom function that replaces the numeric result from the line select dropdown with the proper text name for the line.
if ($line < 1 || $line > 6) {
    for ($i = 1; $i < 7; $i++) {
        $lineName = lineToString($i);
        $completedQuery = $completedQuery."SELECT * FROM en_".$lineName." WHERE MATCH (report,counter) AGAINST ('%".$query."%' IN BOOLEAN MODE)";
        $completedQuery = $completedQuery." UNION ALL ";
    }
    for ($i = 1; $i < 7; $i++) {
        $lineName = lineToString($i);
        $completedQuery = $completedQuery."SELECT * FROM jp_".$lineName." WHERE MATCH (report,counter) AGAINST ('%".$query."%' IN BOOLEAN MODE)";
        if ($i < 6) {
            $completedQuery = $completedQuery." UNION ALL ";
        }
    }
}

$result = mysql_query($completedQuery) or die(mysql_error());
if(mysql_num_rows($result) > 0) {
    while($results = mysql_fetch_array($result)) {
        if ($results['report'] == NULL) {
            echo "<td height=\"25px\"></td>\n";
        } else {
            echo "<td><a href=\"report.php?line=".TABLE-NAME-HERE."&report=".$results['id']."\">".Truncate($results['report'], 100)."</a></td>\n";
        }

[...]

It's in the $completedQuery variable. Basically $completedQuery must be like

$completedQuery = "select * from ".$table_name." where search_term like '%userdata%'";

This means you already know the table name, because you're querying it. You can use $table_name variable in report url.

Maybe you can use mysql_tablename() .

You can look it up at php.net

Still I would prefer the use of PDO..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM