I have a problem, I get this
The structure of my files is
where in config I load the database
<?php
class Database{
public static function connect(){
$db = new mysqli('localhost','root','1234','alumnos');
$db->query("SET NAMES utf8'");
return $db;
}
}
?>
In model I only load the bd class, and load a method which makes a query where it brings the whole table
<?php
require_once "../config/db.php";
class Alumno{
private $id;
private $nombre;
private $grupo;
private $grado;
public function __construct()
{
$this->db = Database::connect();
}
//metodos get y set
public function getAll() //mostrar todo
{
$sql = "SELECT * FROM alumno;";
$post = $this->db->query($sql);
$result = array();
if ($post && mysqli_num_rows($post) >= 1) {
$result = $post;
}
return $result;
}
}
And my index.php file is like this
<?php
header("Access-Control-Allow-Origin: https://localhost:4200");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, Content-Type, Accept,
Access-Control-Request-Method");
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
require_once "../models/alumno.php";
$alumno = new Alumno();
if(isset($_GET['id'])){ //get with id
echo json_encode("with Id");
}else{
echo json_encode($alumno->getAll());
}
?>
It is for my first API in php to consume it in Angular, but it does not load, I know that the problem is not in the Apache installation, because if I change the index to one that only does an echo if it loads, I have no idea. Thanks in advance. .!
To start with, change the path, you have it apparently wrong:
It should be this, looking at the screenshot of your file structure:
because the folder
models
is a child of the folderAlumnos
, where the index.php is, and not "sister", so you don't have to go back one level to find it.To better learn how relative or absolute paths work check out this answer .
Then if more errors come out we will see them.