현재 첫 번째 PHP 애플리케이션을 구축 중입니다. 나는 csv 파일에서 버스 시간을 읽고 자신의 거주지에서 우리 대학교로 다음 버스를 돌려주고 싶다. 그것은 PHP 내 첫 번째 시도, 그래서 온유 :첫 번째 PHP 앱에서 정의되지 않은 오프셋 : 0
<?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]);
}
}
이 함수 쿼리를 사용
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::prepare 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;
}
}
가 사용하는 다른 기능 같아요 관련이 없습니다.
주의 사항 : 지금이 생산 유지 이유를 찾을 수가 캔트 오프셋 정의되지 않은 : 0 /Applications/MAMP/htdocs/html/shuttle.php에 줄에 16
공지 사항 : 오프셋 정의되지 않은 : 0/응용 프로그램을 /MAMP/htdocs/html/shuttle.php 라인 22
는 관련 라인
$residences = query("SELECT * FROM residences WHERE id = ?", $users[0]["residence"]);
및
if($residences[0]["id"] != 0)
있다
도움을 주셔서 감사합니다. :)
편집 :
나는 내 리눅스 시스템에 같은 파일을 전송하고 변경없이 나는 vardump를 얻을. Mac에서 MAMP에 동일한 Vardump를 사용하면 배열이 비어 있습니다.var_dump($users);
array (size=1)
0 =>
array (size=5)
'id' => int 12
'username' => string 'frechdaxx' (length=9)
'mail' => string '*************@gmail.com' (length=23)
'hash' => string '$1$qr5axj4C$BET5zZGJza2DcHI8eD8fV0' (length=34)
'residence' => int 9573
왜이 전혀 문제가 : 지금은 얻을? 함수 테이블은 사용자 테이블에 접근 할 때와 완전히 똑같은 문법으로 작업했습니다. 그리고 우리가 볼 수 있듯이 다른 모든 값을 올바르게 반환합니다.
왜 내 환경이 다른가요? 배열은 MAMP 서버에서는 비어 있지만 Linux에서는 작동합니다. 그러나 쿼리 함수를 사용하는 다른 코드 예제는 두 환경 모두에서 완벽하게 작동합니다.
- 왜 9573의 큰 정수인가? 표의 값은 2입니다.
예를 들어, 다음과 같이 결과를 디버깅하십시오 :'echo '
'; ' – thaJeztah당신의 query()는 결과를 리턴합니까 아니면 DB에만 쿼리를 생성합니까 ??? 쿼리와 결과가 서로 다릅니다. – Svetoslav
가능한 중복 [PHP : "알림 : 정의되지 않은 변수"및 "공지 : 정의되지 않은 인덱스"] (http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) –