rilucu
rilucu

Reputation: 11

How to search between all tables? mysqli

If I put the name of one table it's work, but if I type * it does not find anything.

I trying to search at all the tables that exist in the specific db,

Basically it is for a search box on the site

i need help please

<?php
$link = mysqli_connect("localhost", "***", "***", "***");

if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

if(isset($_REQUEST["term"])){

$sql = "SELECT * FROM * WHERE food LIKE ?";

if($stmt = mysqli_prepare($link, $sql)){

    mysqli_stmt_bind_param($stmt, "s", $param_term);

    $param_term = $_REQUEST["term"] . '%';


    if(mysqli_stmt_execute($stmt)){
        $result = mysqli_stmt_get_result($stmt);


        if(mysqli_num_rows($result) > 0){

            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                echo "<p><a style='color:red' href='http://hidden.com'>" . $row['date'] . "</a></p>";
            }
        } else{
            echo "<p>No matches found</p>";
        }
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}


mysqli_stmt_close($stmt);
}


mysqli_close($link);
?>

Upvotes: 0

Views: 31

Answers (2)

David
David

Reputation: 775

SQL doesn't support wildcarding on tables.

You can run "SHOW TABLES" first which will give you a list of tables in one query result. Then you can iterate through those and run your query on each table individually.

If you need to do it in one shot, you'll need to create a stored procedure which does the same thing, but would all be run on the server-side

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

You must assigne table name esplicitally the use of * ( select all) is not allowed for table name .. you can use just one name or you can use JOIN for join several tables but in your case, assuming each table have a column named food , you could if you need a query that involve mores table you could use union

SELECT * 
FROM table1
WHERE food LIKE ?
UNION 
SELECT * 
FROM table2
WHERE food LIKE ?
UNION 
SELECT * 
FROM table3
WHERE food LIKE ?
......
UNION 
SELECT * 
FROM tablen
WHERE food LIKE ?
";

You can use UNION for distinct result or UNION ALL for get all the result

Upvotes: 1

Related Questions