4
Please help, I’m new to programming and I need to do a table relationship Mysql, with return in JSON, I tried the following code, but it is in error. I already searched a lot on Google and Youtube, but I couldn’t 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 that same structure SQL in my system where I register the data, but I believe that to API, should work differently! Thank you!
In the archive
apiCardapios.php
change everything that starts withmysql_
formysqli_
. That code that’s in the question, it’s from the archiveapiCardapios.php
?– NoobSaibot
I recommend you see this question Why should we not use mysql type functions_*?.
– NoobSaibot
Actually it was an attempt, I need to look for the menu options that are on one table, as the customer selects the meal, which is on another table, on my system, where I enter the options and meals is this code, but for the api, I don’t know how to do, I switched to mysqli, as you suggested, but still with mistakes!
– Priscila Neu