dimanche 8 mai 2016

How to store and process result of a query in PHP array?

My SQLite query in PHP goes like this:

function getTableData($l_name) {
   $db = new PDO("sqlite:../db/mydb.sqlite");
   $results = $db->exec("SELECT * FROM Locations WHERE L_ID = '" . $l_name; . "');";
   .......
}

My questions are:

  • How can I get an array of Locations based on the $results? Could I just write $results[0] or $results[$results.length] to reference first and last records respectively?
  • How would I use a foreach loop to loop through all records? Could I have foreach ($results as $record), where $record is supposed to be a single record of Location in the current iteration?
  • If I were to have a reference to an individual record, say using an array like this records[0], how would I access this record's field (such as L_Name or L_ID)?

Thank you.

Aucun commentaire:

Enregistrer un commentaire