0

bit of a newbie here.

I have the below code which gets data from a data and formats it as a json string. But how can I put this into my js to work with the code there?

PHP / Json encode

<?php include "db.php" ?>

<?php


echo " <br /> Currently Viewing Feedback For 13 <br />  <br />  <br />  <br /> ";



$result = mysqli_query($db_connection, "SELECT * FROM feedback");



while ($row = $result->fetch_assoc()){

  echo "<br /> <br /> <br />";
  echo json_encode($result);
}





?>

1 Answer 1

1

right here, you just try to encode the mysqli_result into json that's not the right thing to do.. And, don't try to put HTML into your JSON response ;)

<?php include "db.php"


echo " <br /> Currently Viewing Feedback For 13 <br />  <br />  <br />  <br /> ";



$result = mysqli_query($db_connection, "SELECT * FROM feedback");

$finalResult = array();

while ($row = $result->fetch_assoc()){

  $finalResult[] = $row;

}

  echo json_encode($finalResult);
?>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. Tried your code and the array is showing up as {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null} Should this not be the data from the db? Sorry if i'm asking a really silly question.
Really silly question for me : Is there data in your table ? Try to use the mysqli_fetch_array instead of fetch_assoc
Yep got data in there. Tried the mysqli_fetch_array, no luck nothing shows up.
My bad : See the modification (json_encode($finalResult)) If it dont work try : var_dump($result); var_dump($result->num_rows); If the object still show null, the problem came from the mysql_result object : it does not retrieve data from the server until it's actually required
Thanks Yeppao! Works :) Would you know how I could convert this string into a table using JS?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.