my goal here is to be able 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’ve rtm and I haven’t found anything combining fetch_assoc with prepared statements, so I’m not sure if it’s even possible. Any help to get this working is appreciated, here’s my code currently.
$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';
From the PHP website page for mysqli->prepare (with emphasis added to the most relevant part):
Assuming you can get past that problem, your use of
mysqliis a little confused. You correctly bind your parameters and execute, but you’ve mixed up two different ways of getting at your results. Eithermysqli_stmt_get_resultto fetch the result set and then usemysqli_fetch_assocon that, ormysqli_stmt_bind_result, and then usemysqli_stmt_fetchto fetch the next set of results into your bound variables. (Usually you’d iterate over the results using something likewhile(mysqli_stmt_fetch($stmt)){ //do stuff here }