mysql - How to do Inner Join with multiple rows return Json in php -


i have 2 table 1 wich has mulitple rows in reference other table.

questions_table:

id | value_question --------- 1  | "first question" 2  | "second question" 

responses_table:

id | question_id| value_response | is_true ------------------------------------------- 1  | 1          | sfu            | true 2  | 1          | ubc            | false 3  | 2          | bu             | true 4  | 2          | ri             | false 

i know wich best practice return each rows questions_table in json php :

[   { "value_question": "first question",     "responses": [       {"value_response": "sfu", "is_true": "true"},       {"value_response": "ubc", "is_true": "false"}     ],   },   { "value_question": "first question",     "responses": [       {"value_response": "sfu", "is_true": "true"},       {"value_response": "ubc", "is_true": "false"}     ],   }, ] 

i'm trying in mysql reach sort string.

select       value_question,      concat('[',group_concat('{value_response:',responses.value_response,', is_true:',responses.is_true,'}'),']')'responses'  questions inner join responses on       questions.id = responses.question_id group id_question 

i don't know better beetween doing in php or mysql if need more details tell me.

sorry english, i'm french guy :)

thanks know how use json_encode, question more : best request have structure described.

the php :

    include 'connect_db.php';      $sql= *the request*      $stmt = $dbh->prepare($sql);     $stmt->execute();     $result = $stmt->fetchall(pdo::fetch_assoc);     $json = json_encode($result);      echo $json; 

please use below code.
database connection please replace mysql database server values in mysqli_connect function.
have used mysql query , php mysqli, since not aware of pdo. first taking request in variable, run mysql join query. iterate on mysql fetched result , build array. json_encode.

$con = mysqli_connect('db_host', 'db_username', 'db_password', 'db_name'); // please replace mysql server values.  $value_question = 'first question'; // *the request*  $value_question = $con->real_escape_string($value_question); // escape values  $query =  " select `responses_table`.`value_response` , `responses_table`.`is_true`              `questions_table`              inner join `responses_table`              on `questions_table`.`id` = `responses_table`.`question_id`              `questions_table`.`value_question` = '$value_question' ";   $result = $con->query($query);  while( $row = $result->fetch_assoc() ){ // build responses array     $responses[] = array( 'value_response' => $row['value_response'] ,                              'is_true'       => $row['is_true'])  ; } $response = array('value_question' => $value_question ,                     'responses' => $responses );  echo json_encode($response); // json response 

Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -