Mysqli prepared statement with fetch_assoc

my goal here is to get a variable (with php) and use it in a prepared statement (with mysqli) and then fetch_assoc. For some reason, this code will not work (no errors). I have rtm and I haven’t found anything combining fetch_assoc with prepared statements, so I'm not sure if this is possible. Any help to get this work appreciated, here is my code for now.

$where = $_GET['section'];
            $mysqli = mysqli_connect("localhost", "root", "","test");

            if($stmt = mysqli_prepare($mysqli,"SELECT title, img, active, price FROM ? ORDER by ID limit 5 ")){
                mysqli_stmt_bind_param($stmt, 's', $where);
                mysqli_stmt_execute($mysqli);
                mysqli_stmt_fetch($mysqli);
                 while($row = mysqli_fetch_assoc($stmt)){
                    if($row['active']=="yes"){
                        echo 'the rest of my stuff goes here';
+5
source share
2 answers

On the PHP website page for mysqli-> prepare (with an accent added to the most important part):

Note:

SQL. , VALUES() INSERT ( ) WHERE .

(, ), , , SELECT) . , . , (DML) , (DDL).

, , mysqli . , .

  • mysqli_stmt_get_result , mysqli_fetch_assoc,
  • mysqli_stmt_bind_result, mysqli_stmt_fetch, . ( , - while(mysqli_stmt_fetch($stmt)){ //do stuff here }
+5

, :

$mysqli=new mysqli("host","user","pass","db");
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $variable);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
....
}
-1

All Articles