0

i am trying selecting(B) data from mysql in another selecting(A), but i get an error from the selecting(B). I have no idea how to fix it and find the problem of it.

// the server connection code id omitted. 

// the code below has been edited. $bookId= $_POST["bookId"];

$sql = "SELECT * FROM feedback WHERE bookId= '".$bookId."'";// selecting(A)
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {

            // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $sql = "SELECT * FROM userInfo WHERE userId= '". $row["userId"]."'"; //selecting(B)
        $data = mysqli_query($conn, $sql);
        $userData = mysql_fetch_array($data);

        $Arr = array("userId" => $userData ["userId"], "userName" => $userData ["userName"],
               "feedback" => $row["feedback"], "date" => $row["date"]);

         $feedbackArray[] = array("feedbackDetail"=> $Arr);
    }
} 

mysqli_close($conn);
$json= array("feedback" => $feedbackArray);
echo  json_encode($json);

the result i got: an error + a valid json (but has null:( )

{"feedback":[{"feedbackDetail":{"userId":null,"userName":null,"feedback":"I love you ba~~","date":"2016-03-19 21:48:46"}},{"feedbackDetail":{"userId":null,"userName":null,"feedback":"well!!!","date":"2016-03-19 21:46:52"}}]}

any idea?

king yau
  • 500
  • 1
  • 9
  • 28
  • 2
    You're mixing MySQL libraries by using `**mysqli**_query` and `**mysql**_fetch_array`. Change the latter to `mysqli_fetch_array` and it should work. Your code is also vulnerable to SQL injection. Please read [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) for information on how to fix it. – Matt Raines Mar 19 '16 at 14:15
  • Do you have foreign keys between feedback and userInfo tables, the field UserId in feedback is not null? – Mauricio Florez Mar 19 '16 at 14:18
  • – Matt Raines Thank you very much! i was just missing a 'i' . – king yau Mar 19 '16 at 15:53

0 Answers0