How do I relate tables with json returns to php?

Asked

Viewed 369 times

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 with mysql_ for mysqli_. That code that’s in the question, it’s from the archive apiCardapios.php?

  • I recommend you see this question Why should we not use mysql type functions_*?.

  • 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!

1 answer

1

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

Procedural style is the one you’re 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 in time to use the method mysqli_query note that in the above example, it receives two parameters, because in the procedural style it is necessary to inform the parameter $link.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.