I currently have an AngularJS application querying a database through a factory linked to a PHP script. The script below works exactly as planned and outputs the JSON used for Angular:
<?php
function getCompanyDetails($company_id)
{
// Connect to the database.
require_once("config/connection.php");
$query = "SELECT id, header, content FROM company_details WHERE company_id = ?;";
$stmt = $conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param("i", $company_id);
$stmt->execute();
$data = $stmt->get_result();
while($row = $data->fetch_assoc()) {
$result[] = $row;
}
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$stmt->close();
mysqli_close($conn);
echo json_encode($result);
}
}
getCompanyDetails(1);
?>
Now, I have other sections of the application which require the exact same action (Selecting results from the database) and I am using the following script (an almost identical copy to the one above):
<?php
function getCompanyProjects($company_id)
{
// Connect to the database.
require_once("config/connection.php");
$query = "SELECT title, description FROM company_projects WHERE company_id = ?;";
$stmt = $conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param("i", $company_id);
$stmt->execute();
$data = $stmt->get_result();
while($row = $data->fetch_assoc()) {
$result[] = $row;
}
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$stmt->close();
mysqli_close($conn);
echo json_encode($result);
}
}
getCompanyProjects(1);
?>
The problem is, the second script will not return any results. There are records in the database for the table and I believe I am closing the connection after each use. I have been stuck on this for a while now, does anybody have any idea of what the problem could be?
The query SELECT title, description FROM company_projects WHERE company_id = ?;
works on phpMyAdmin.
Thanks in advance.