How can I relate tables with return in json with php?

2

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!

    
asked by anonymous 06.11.2017 / 02:19

1 answer

0

According to documentation of MySQLi , you can use the query method in two ways:

  

Procedural style is the one you are using.

$con = new mysqli("servidor", "usuario", "senha", "banco");
$qry = "SELECT id, nome, email FROM perfil";
/* Verifica a conexão */
if (mysqli_connect_errno()) {
    printf("Falha na conexão com a base de dados: %s\n", mysqli_connect_error());
    exit();
}
$res = mysqli_query($con, $qry); // Executa a query e armazena seu resultado
printf("Retornou %d cadastros.\n", mysqli_num_rows($res)); // Imprime na tela a quantidade de registros encontrado na tabela

Your problem is just at the time of using the mysqli_query method, since in the example above, it receives two parameters, since in the procedural style it is necessary to inform the $link parameter.

  

Object oriented style .

$con = new mysqli("servidor", "usuario", "senha", "banco");
$qry = "SELECT id, nome, email FROM perfil";
/* Verifica a conexão */
if ($con->connect_errno) {
    printf("Falha na conexão com a base de dados: %s\n", $con->connect_error);
    exit();
}
$res = $con->query($qry); // Executa a query e armazena seu resultado
printf("Retornou %d cadastros.\n", $res->num_rows); // Imprime na tela a quantidade de registros encontrado na tabela

Reference

07.11.2017 / 02:17