PHP PDO error undefined index serial -


i'm pull hair out. can't undefined index go away. says echo htmlspecialchars($r['serial']) want list item out of database table.

<?php     try{             $conn = new pdo("mysql:host=$sql_server;dbname=$sql_db", $sql_user, $sql_pass);         $sql = "select serial, model, devicecondition, sealcondition, location, deploydate, weight, notes $sql_table order serial";         $q = $conn->prepare($sql);          $q->setfetchmode(pdo::fetch_obj);          while ($r = $q->fetch());     } catch (pdoeexception $pe) {         die("could not connect database" . $pe->getmessage());     }    ?>        </div>  <?php $r = $q->fetchall(); echo htmlspecialchars($r['serial']) ?> 

see below code, fetchall results in associative array, can data $row['serial'] , use later. added execute() on pdo statement obj.

<?php  try{          $conn = new pdo("mysql:host=$sql_server;dbname=$sql_db", $sql_user, $sql_pass);      $sql = "select serial, model, devicecondition, sealcondition, location, deploydate, weight, notes $sql_table order serial";      $q = $conn->prepare($sql);       $q->execute(); // make sure add       $results = $q->fetchall();      foreach($results $row) {         echo htmlspecialchars($row['serial']) . "<br>";         //if want use serial later, store array         $serials [] = htmlspecialchars($row['serial']);     }  // while ($r = $q->fetch()); not needed because of fetchall statement above  } catch (pdoeexception $pe) {         die("could not connect database" . $pe->getmessage());     } ?>  </div>  <?php     //$r = $q->fetchall(); not needed here, did above ?>  <?php      //echo htmlspecialchars($r['serial']); use array above     print_r($serials); ?> 

let me know if helps


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -