简体   繁体   中英

How would i go about changing a MySQL array by comparing it to another?

I want to display a score coming from my database through a json dataset (from the table "score"), which works fine, but my quiz_id is the foreign key, which means the dataset will contain the id, and not the name of the quiz. Which doesn't look good on the CanvasJS graph. The quiz_name is located in the quiz table, with the primary key quiz_id. How would i make the json dataset include the quiz_name instead of quiz_id?

my test.php, which is creating the json:

<?php

header('Content-Type: application/json');

$con = mysqli_connect("123.123.123.123", "Seba0702", "", "kayeetdb");


    $data_points = array();

    $result = mysqli_query($con, "SELECT * FROM score");

    while($row = mysqli_fetch_array($result))
    {        


       $point = array("label" => $row['quiz_id'] , "y" => $row['quiz_score']);

       array_push($data_points, $point);        
    }




    echo json_encode($data_points, JSON_NUMERIC_CHECK);

mysqli_close($con);

?>


My tables: Quiz Table:

测验表

Score Table:

得分表

I want the json to include the quiz_name and quiz_score.

For retrieve infor from another table you need a join

SELECT score.quiz_id, score.student_id, score.quiz_score, quiz.name
FROM score
INNER JOIN quiz on quiz.quiz_id = score.quiz_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM