function fetchbyId($tableName,$idName,$id){
        global $connection;
        $stmt = mysqli_prepare($connection, 'SELECT * FROM ? WHERE ? = ?');
        var_dump($stmt);
        mysqli_stmt_bind_param($stmt,'s',$tableName);
        mysqli_stmt_bind_param($stmt,'s',$idName);
        mysqli_stmt_bind_param($stmt,'i',$id);
        $stmt = mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($name,$id);
        $fetchArray = array();
        while($row = mysqli_stmt_fetch($stmt)){
            $fetchArray[] = $row;
        }
        return $fetchArray;
    }


我可以将占位符用于表名称,还是只能用于表列?

最佳答案

不,它只接受值(即:不接受列,表名,模式名和保留字),因为它们将被转义。您可以这样做:

$sql = sprintf('SELECT * FROM %s WHERE %s = ?', $tableName, $idName);
$stmt = mysqli_prepare($connection, $sql);
mysqli_stmt_bind_param($stmt,'i',$id);

关于php - mysql准备好的语句,这可能吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4479287/

10-11 06:32