Method to print MySQL row in PHP using foreach statement? -


i new php , programming, attempting print out results each row of 1 of mysql database tables.

using set of while loops achieved this:

while($placeholder = $query->fetch_assoc()){ // use assoc     echo "<div class='placeholder'><img src=" . $placeholder['photo']. " /></div>";      echo "<br />"; }  while($placeholder2 = $query2->fetch_assoc()){     echo "<div class='placeholder2'>" . $placeholder2['name'] . "</div>";      echo "<br />"; }  //etc, etc... 

besides being inefficient, i'm assuming may security risk.

is there better way this, possibly using foreach statement?

here sql code forgot include:

$query = mysqli_query($conx, $sql); $query2 = mysqli_query($conx, $sql);  $sql = ('select id,name,picture table order name desc limit 50'); 

i dont think using foreach statement print out results make less safe/unsafe. depends on query. should able print of them using foreach doing like:

$result = $query->fetch_all(mysqli_assoc); foreach($result $row) {      echo $row['photo']; } 

note** use fetch_all need mysqlnd installed

edit: looking @ query nothing can injected there. no outside variables being used in query, safe. use while loop posted.

edit 2: link fetch_all documentation: http://php.net/manual/en/mysqli-result.fetch-all.php


Comments