SELECT * from SQL table using prepared statement

后端 未结 4 2035
梦谈多话
梦谈多话 2021-01-13 22:00

I\'m using a prepared statement to SELECT * from a MySQL table and I\'m not sure how to use while($row = mysqli_fetch_array($stmt)) to loop through

4条回答
  •  轮回少年
    2021-01-13 22:20

    Nothing wrong with Darwin's answer, but wanted to point out PDO as an alternative with much lighter syntax:

     PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    $link = new PDO("mysql:host=$host;dbname=$db", $username, $password, $options);
    $stmt = $link->prepare("SELECT * from `wp_posts` WHERE ID=?");
    $stmt->execute([$pid]);
    $result = $stmt->fetchAll();
    
    // Now you have a plain array to work with, database work is over
    foreach ($result as $row):
    ?>
    
    



    No need for any binding at all, and personally I find it much easier to work with.

提交回复
热议问题