php - MySQLi query looping over array returns throws Undefined offset: 1 -


i performing queries. problem looping on array throws

undefined offset: 1

this code used:

$id=1; $books=""; $prep_stmt= "select * books id=?"; $stmt = $mysqli->prepare($prep_stmt); $stmt->bind_param('i', $id); $stmt->execute(); if (!$stmt) {     die('there error running query [' . $mysqli->error . ']');     exit(); } $meta = $stmt->result_metadata(); while ($field1 = $meta->fetch_field()) {     $parameters[] = & $row[$field1->name]; } call_user_func_array(array($stmt, 'bind_result'), $parameters);  while ($stmt->fetch()) {     foreach ($row $key => $val) {         $x[$key] = $val;     }     $results[] = $x; } if ($stmt->num_rows>0){     ($i = 0; $i <= sizeof($results[0]) - 1; $i++) {         $id=$results[$i]['id'];         $name=$results[$i]['name'];         $books=$books."<option value='".$id."'>".$naziv."</option>";     } 

you have array of rows in $results when looping loop 0 number of columns:

$i <= sizeof($results[0]) 

if number of columns greater number of results undefined offset. try this:

for ($i = 0; $i <= sizeof($results) - 1; $i++) {     $id=$results[$i]['id'];     $name=$results[$i]['name'];     $books=$books."<option value='".$id."'>".$naziv."</option>"; } 

or, more succinctly:

foreach($results $result) {     $id=$result['id'];     $name=$result['name'];     $books=$books."<option value='".$id."'>".$naziv."</option>"; } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -