Please help me, I'm new to programming and I need to do a MySQL table relationship, with return in JSON , I tried the following code but it is with errors. I've done a lot of research on Google and Youtube , but I could not find anything like it.
Code:
<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');
$con = new mysqli('mysql.meusite.com.br', 'meubanco', 'senha', 'meubanco');
if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());
$sql = "SELECT refeicao.nome, refeicao.id FROM refeicao_refeicao AS refeicao
INNER JOIN refeicao_alimento AS opcao
INNER JOIN alimentos_refeicao AS cardapio
WHERE cardapio.id_refeicao = refeicao.id
AND cardapio.id_alimentos = opcao.id
GROUP BY refeicao.id";
$query = mysql_query($sql);
$arr = Array();
if(mysql_num_rows($query)){
while($dados = mysql_fetch_object($query)){
$arr[0] = $dados->id;
$arr[1] = $dados->nome;
$arr[2] = $id_refeicao;
}
$sql2 = "SELECT refeicao_alimento.nome
FROM refeicao_alimento
INNER JOIN refeicao_refeicao
INNER JOIN alimentos_refeicao
WHERE alimentos_refeicao.id_alimentos = refeicao_alimento.id
AND alimentos_refeicao.id_refeicao = $id_refeicao
GROUP BY refeicao_alimento.id";
$query2 = mysql_query($sql2);
$arr2 = Array();
if(mysql_num_rows($query)){
while($dados2 = mysql_fetch_object($query)){
$arr2[0] = $dados2->nome;
}
}
echo json_encode();
//print_r($JSON);
}
?>
Initial Error:
<br />
<b>Warning</b>: mysql_query(): No such file or directory in <b>/home/qualitserv/www/api/apiCardapios.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>: mysql_query(): A link to the server could not be established in <b>/home/qualitserv/www/api/apiCardapios.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>: mysql_num_rows() expects parameter 1 to be resource, boolean given in <b>/home/qualitserv/www/api/apiCardapios.php</b> on line <b>20</b><br />
I already use this same SQL structure on my system where I register the data, but I believe that for API , it should work differently! Thank you!