在MYSQL SELECT中连接PHP变量

时间:2014-02-10 14:52:35

标签: php mysql mysqli

我试图在MYSQL SELECT QUERY中连接一个PHP变量。但到目前为止没有任何效果。

$nkFetch = function($tableNo) use ($link){

    $tableQuery = 'SELECT * FROM table.$tableNo'; <------------this

    $res = mysqli_query($link, $tableQuery);

    if (!mysqli_query($link, $tableQuery)) {
        printf("Errormessage: %s\n", mysqli_error($link));
    }

    $table = array();

    while ($row = mysqli_fetch_array($res)){
        $table[] = $row;
    }  

    shuffle($table);

    foreach( $table as $row ){
        echo "<li>" . $row['WORD'] . "</li>";
    }

}

1 个答案:

答案 0 :(得分:8)

$tableQuery = "SELECT * FROM table.$tableNo";

$tableQuery = 'SELECT * FROM table.' . $tableNo;

$tableQuery = "SELECT * FROM table.{$tableNo}";

$tableQuery = sprintf("SELECT * FROM table.%s", $tableNo);