<?php
mysql_pconnect("localhost","root","");
mysql_select_db("AppDevelopMenu") or die( '1');
$sql1=mysql_query("select Question from questions where QId ='1'");

if(!$sql1)
echo '2';

else{

$question=mysql_fetch_assoc($sql1) ;
$outputq[]=$question;


$sql=mysql_query("select OptionId,OptionContent from options where QId ='1'");

if(!$sql)
{
echo '3';
}

else {
while($row=mysql_fetch_assoc($sql))
$outputq[]=$row;
print(json_encode($outputq));
}
}

mysql_close();

?>

i have to modify this code to read json in a different structure that lets say question is the root node..QuestionContent and options are there on next level.And then options is the root node for option1,option2,option3 etc
How should I modify my php code to achieve this

Dani AI

Generated

Brief summary and a safer approach: asked for JSON with a top-level question node containing QuestionContent and an options child where each option is named option1, option2, etc. pointed in the right direction by nesting before json_encode. The original code uses deprecated mysql_* calls and misses JSON headers, charset and error checks. A better pattern is to use PDO/mysqli, prepare the nested structure (or an object of optionN keys if the client needs those exact names), and return proper JSON with UTF-8.

Example (builds question -> QuestionContent and options -> option1, option2...):

<?php
$pdo = new PDO(
  'mysql:host=localhost;dbname=AppDevelopMenu;charset=utf8mb4',
  'dbuser','dbpass',
  [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC]
);

$qid = (int)($_GET['qid'] ?? 1);

$stmt = $pdo->prepare('SELECT Question FROM questions WHERE QId = :qid');
$stmt->execute([':qid'=>$qid]);
$question = $stmt->fetch();

if (!$question) {
  header('Content-Type: application/json; charset=utf-8');
  echo json_encode(['error'=>'Question not found']);
  exit;
}

$stmt = $pdo->prepare('SELECT OptionId, OptionContent FROM options WHERE QId = :qid ORDER BY OptionId');
$stmt->execute([':qid'=>$qid]);
$options = $stmt->fetchAll();

$result = ['question'=>['QuestionContent'=>$question['Question'],'options'=>[]]];
$i = 1;
foreach ($options as $opt) {
  $result['question']['options']['option'.$i++] = ['id'=>(int)$opt['OptionId'],'content'=>$opt['OptionContent']];
}

header('Content-Type: application/json; charset=utf-8');
echo json_encode($result, JSON_UNESCAPED_UNICODE);

Quick tips: prefer an indexed array (["options":[...]) unless the client specifically needs option1 keys — arrays are easier to iterate. Always set the DB charset to utf8mb4, use prepared statements, check json_last_error_msg() after encode (or JSON_THROW_ON_ERROR when available), and return consistent types (cast IDs to int).

Member Avatar for Member #120589

Not sure if this is what you want:

<?php
$qid = 1;
$sql1=mysql_query("select Question from questions where QId = $qid");
if(!$sql1){
    echo '2';
}else{
    $question=mysql_fetch_assoc($sql1) ;
    $outputq['question'] =$question['Question'];
    $sql=mysql_query("select OptionId,OptionContent from options where QId = $qid");
    if(!$sql){
        echo '3';
    }else{
        while($row=mysql_fetch_assoc($sql)){
            $outputq['options'][] = array('id'=>$row['OptionId'], 'content'=>$row['OptionContent']);
        }
    }
    print(json_encode($outputq));
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.