Unanswered Webservice (PHP, Mysql, JSON)

Asked

Viewed 410 times

0

I have a PHP webservice with a Mysql database and JSON responses. It is working normally, but I have a problem using the following query:

    SELECT podcast.id, podcast.nome, podcast.descricao, autor.nome, categoria.nome_cat 
FROM podcast 
    JOIN autor ON autor.id = podcast.autor_id
    JOIN categoria ON categoria.id = podcast.categoria_id;

I tested the above query in Mysql and it works.

Below is the code of the webservice:

    <?php

/*
 * Following code will list all the podcasts
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// get all podcasts from podcasts table
$result = mysql_query("SELECT podcast.id, podcast.nome, podcast.descricao, autor.nome_autor, categoria.nome_cat FROM podcast 
JOIN autor ON autor.id = podcast.autor_id
JOIN categoria ON categoria.id = podcast.categoria_id;
 ") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // podcast node

    $response["podcast"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $podcast = array();
        $podcast["id"] = $row["id"];
        $podcast["nome"] = $row["nome"];
        $podcast["descricao"] = $row["descricao"];
        $podcast["nome_cat"] = $row["nome_cat"];
        $podcast["nome_autor"] = $row["nome_autor"];

        // push single podcast into final response array
        array_push($response["podcast"], $podcast);
    }

    // echoing JSON response
    echo json_encode($response);

} //else {
    // no podcasts found
    //$response["success"] = 0;
    //$response["message"] = "No podcasts found";

    // echo no users JSON
    //echo json_encode($response);
//}
?>

The Query I quoted at the beginning of the post shows no response in JSON, with the following message:

Response does not contain any data.

I did some tests and it’s passing both if and while. If I print the answer on the screen using print_r, I get the information I’d like.

Could someone help me?

  • See if you are in if, print the number of lines of the result: echo mysql_num_rows($result);

  • @Gabrielsantos thanks for the help. Yes, you are entering if and printing the number '2'. I have exactly two records in the table.

  • Instead of mysql_fetch_array use the mysql_fetch_assoc I think this is it ...

  • Hello, I’ve already said that print_r($Response) shows the right array. What does echo json_encode($Response) do; when only runs php page? If it’s okay we need javascript

No answers

Browser other questions tagged

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