uneeb meer uneeb meer - 6 months ago 14
SQL Question

using variable inside php query

hi i want to use different tables in from clause sql query every time in a loop issue is it is not accepting my code here is the code


$sql="select * from $tbl'+$i' where client_id='$id'";

echo mysql_error($db);


//echo mysqli_error($db);

what is the issue ? i want to use different table for every iteration for comparison.thanks in advance


Try using something like this. Table names in an array, foreach over that array and process the results accordingly.

$id = '';   // not sure where you are getting this from
$db = '';   // again this was not in your example above

$tables = [
foreach ($tables as $table) {
    $sql = sprintf("SELECT * FROM `%s` WHERE client_id = %d", $table, $id);

    if (!$result=mysqli_query($db,$sql)) {
        echo mysql_error($db);
        // or break if you want to still check the other tables.

    // do something with the result here ($result)