0
<?php
require 'config.php';
$db = new connect();
$students = $db->select("students","ORDER BY students_id ASC");
if( $students != null ){
foreach ($students as $student) {
$student['error'] = '';
$output[] = $student;
}}else{
$student['error'] = 'No students...';
$output[] = $student;
}
echo json_encode($output);
?>

if the database is empty i have error. Notice: Undefined variable: output

1 Answers1

1

Make sure that you initialise $output as an empty array first,

$db = new connect();
$output = array();
$students = $db->select("students","ORDER BY students_id ASC");

Your code only ever initialises $output if the foreach loop runs, which it won't with no results from the database

iJamesPHP2
  • 524
  • 4
  • 13
Illya
  • 1,268
  • 1
  • 5
  • 16