Can not echo data from MYSQL database

php code to retrieve data from mysql database and display
how to retrieve data from database in php using mysql
how to fetch single row data from database in php
how to display data from database in php in table
how to fetch data from database in php and display in html table
how to fetch data from database and store in variable in php
php mysql fetch all rows into array
fetch data from database in php mysqli

Hello! The data in table is like that, and I cannot echo these entries individually. I want to show this data in HTML table like for example, Subject: Math, Correct: 34, Wrong: 6.

I am new in php, please help me with this case.

The data in the table seems to be in JSON format. You can convert it to a PHP object using json_decode() then print that object using print_r().

When put all together, you get

echo print_r(json_decode($data), true);

Note, I have to edit this response back to what it is now because @fenil-shah had changed it some something else that was not suitable. Please send your own response instead of making others say what they didn't intend to.

@fenil-shah, my answer was written the way it was on purpose. Here are the explanations:

  1. In general, if you want to stay in control of the result of the print_r() function, you need to return its result as a string. That's why I have passed true as second parameter for the print_r() function. You wouldn't using print_r() without returning its result as a string in a middle of another script because it will output some text in an uncontrolled manner that can break an add. Once you have the return string, you can send it to a log file or do whatever you want with it.
  2. json_decode works beautifully without passing it a true as second parameter. The OP's example is showing a object. You edit was purposely changing the decoded object to an array with associative array. Why?
  3. You have removed the space after the comma which you had moved in from the print_r() to the json_decode() to make it look like json_decode($data,true). You need to know that the space was there for two reasons: Readability and Compliance with the PSR-2 standard (read more about that PHP coding standard at https://www.php-fig.org/psr/psr-2/#46-method-and-function-calls).

Connected to MySQL database but cannot print the data, die('Cannot connect with the database.');} // Check connection if ($db-> connect_error) { die("Connection failed: " . $conn->connect_error); } echo " Connected� I'm looking to write a very simple script to echo out MySQL data into an HTML table using the following query. I haven't coded in PHP in about a decade, however, and I've forgotten a lot.

There are echo "",print_r() and print() function available in php to print data.If query is returning array then use print_r() function but if query is not returning anything or invalid fetch method in php then data is not getting display by any of these functions.

mysql_fetch_row - Manual, See also MySQL: choosing an API guide and related FAQ for more information. mysql_fetch_row() fetches one row of data from the result associated with the specified echo 'Could not run query: ' . mysql_error(); require_once 'db.php'; 36-40: If that result contains more than 0 rows then display that data in an HTML table. 43, 44: If the result contains 0 rows then give a message “0 results” and close a database connection. And this is how we fetch data from the database and display it on HTML table using PHP and MySQL database.

I think this is what you want as a output. You can decode the JSON received from database using json_decode() function and get your result transformed in associative array. Later on passing the array in foreach() loop will do your work accordingly

 <?php
    $result = '{
      "30": {
      "subject_id":343,
      "correct_answers":34,
      "wrong _answers":61,
      "not_answered":0,
      "time_spent":3801,
      "time_to_spend":3680,
      "time_spent_correct_ answers":3286,
      "time_spent_wrong_answers":515 
      },
      "52": {
        "subject_id":52,
        "correct_answers":7,
        "wrong_answers":3,
        "not_answered":0,
        "time_spent":883 ,
        "time_to_spend":94343,
        "time_spent_correct_ans wers":352,
        "time_spent wrong_answers":441
      },
      "53": {
        "subject_id":53,
        "correct_answers":3,
        "wrong_answers":7,
        "not_answered":43,
        "time_spent":584 ,
        "time_to_spend":900,
        "time_spent_correct_ans wers":154,
        "time_spent wrong_answers":430
      }
    }';
    $json_decoded_data = json_decode($result,true);
    ?>
    <table>
        <tbody>
            <tr>
                <th>subject_id</th>
                <th>correct_answers</th>
                <th>wrong_answers</th>
            </tr>
            <?php
            foreach($json_decoded_data as $row){
                echo "<tr>";
                echo "<td>".$row['subject_id']."</td>";
                echo "<td>".$row['correct_answers']."</td>";
                echo "<td>".$row['correct_answers']."</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
</table>

mysql_query - Manual, Data inside the query should be properly escaped. private $connection; // The MySQL database connection Warning: mysql_query() [function.mysql-query]: Unable to save result set in filename.php string to a MySQL query can be misleading if you use echo($sql) in PHP because the null terminator may not be visible. Just follow the below two steps and fetch or get data from MySQL database in PHP: Connect to MySQL database; Fetch data from the database; 1. Connect to MySQL database . In this step, you will create a file name db.php and update the below code into your file. The below code is used to create a MySQL database connection in PHP. When we fetch, insert, update or delete data from MySQL database, there we will include this file:

Getting Data From MySQL Database, Getting Data From MySQL Database - Data can be fetched from MySQL tables by executing SQL SELECT This function returns FALSE if there are no more rows. Try out following example to display all the records from employee table. For retrieve data from MySQL the SELECT statement is used. We can retrieve data from specific column or all column of a table. To retrieve selected column data from database the SQL query is

PHP MySQL Select Data, Select Data From a MySQL Database. The SELECT statement is used to select data from one or more tables: SELECT echo "0 results"; } $conn->close(); ?>. Data can be fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. You have several options to fetch data from MySQL. The most frequently used option is to use function mysql_fetch_array (). This function returns row as an associative array, a numeric array, or both. This function returns FALSE if there are no more rows.

PHP MySQL: Querying Data from Database, You will learn how to query data from MySQL database by using PHP PDO and use PDO After that, fetch each row from the result set until there is no row left by using the fetch() td> <td><?php echo htmlspecialchars($row['firstname']); ?>� MySQL Database MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data PHP XML PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM PHP

Comments
  • Please post code / json in text and not image.