I'm currently building my first PHP application. I want to read in bus times from a csv file and give back ti users the next bus from their residence to our university. It is my first try with PHP, so be gentle:\[code\]<?php // configuration require("../includes/config.php"); if (!empty($_SESSION["id"])) { //lookup database entries for house $users = query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]); if ($users === false) { apologize("Sorry, there was an error"); } //lookup database entries for house $residences = query("SELECT * FROM residences WHERE id = ?", $users[0]["residence"]); if ($residences === false) { apologize("Sorry, there was an error"); } //if user specified a residence in his profile if($residences[0]["id"] != 0) { $times = array(); //if there is no bus today, in this case sat and sun if(date( "w", $timestamp) == 0 || date( "w", $timestamp) == 6) { $times[0] = "There is no bus today"; } //load the busplan for his residence else { //open file and load in array if time is higher than date("His"); $timesList = file_get_contents($users[0]["residence"] . ".csv"); $nextbuses = explode(',', $timesList); $hoursMins = date("Gi"); $num = 0; for($i = 0; $i < count($nextbuses); $i++) { if($hoursMins < $nextbuses[$i]) { $times[$num] = $nextbuses[$i]; $num++; } } } render("shuttle_show.php", ["title" => "Next Shuttle from your residence.", "times" => $times]); } }\[/code\]This uses the function query:\[code\]function query(/* $sql [, ... ] */){ // SQL statement $sql = func_get_arg(0); // parameters, if any $parameters = array_slice(func_get_args(), 1); // try to connect to database static $handle; if (!isset($handle)) { try { // connect to database $handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD); // ensure that PDO:
repare returns false when passed invalid SQL $handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } catch (Exception $e) { // trigger (big, orange) error trigger_error($e->getMessage(), E_USER_ERROR); exit; } } // prepare SQL statement $statement = $handle->prepare($sql); if ($statement === false) { // trigger (big, orange) error trigger_error($handle->errorInfo()[2], E_USER_ERROR); exit; } // execute SQL statement $results = $statement->execute($parameters); // return result set's rows, if any if ($results !== false) { return $statement->fetchAll(PDO::FETCH_ASSOC); } else { return false; }}\[/code\]the other functions it uses are not relevant I guess. Now I cant seem to find why this keeps producing:Notice: Undefined offset: 0 in /Applications/MAMP/htdocs/html/shuttle.php on line 16Notice: Undefined offset: 0 in /Applications/MAMP/htdocs/html/shuttle.php on line 22The relevant lines are \[code\] $residences = query("SELECT * FROM residences WHERE id = ?", $users[0]["residence"]);\[/code\]and \[code\] if($residences[0]["id"] != 0)\[/code\]Would appreciate some help! ![Smile :) :)](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f642.png)
![Stick Out Tongue :p :p](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f61b.png)
![Smile :) :)](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f642.png)