0

I am trying to display the contents of a "users" table in my MYSQL database using PHP,JQUERY and JSON.

Here is the PHP file:

<?php

  $host = "localhost";
  $user = "root";
  $pass = "";

  $databaseName = "ITSM";
  $tableName = "signup_and_login_table";


  include 'database_connection.php';
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);

 $result = mysql_query("SELECT * FROM $tableName");           
 $array = mysql_fetch_row($result);                          


  echo json_encode($array);

?>

On my HTML page i have a simple table im trying to target:

<table id="personDataTable">
        <tr>
            <th>Id</th>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>

    </table>

This is the jquery ajax, I want it to loop through all the users and other table entities to display all the contents of the Database table on the page, Im currently just getting "undefined".

 $(function ()
 {


  $.ajax({
     url: 'CMGetdata.php',                  
     data: "",                        
     dataType: 'json',                
     success: function(data, textStatus, jqXHR) {

        drawTable(data);
    }
   });

 });

function drawTable(data) {
    for (var i = 0; i < data.length; i++) {
        drawRow(data[i]);
    }
}

console.log("test");
function drawRow(rowData) {
    var row = $("<tr />")
    $("#personDataTable").append(row); 
    row.append($("<td>" + rowData.id + "</td>"));
    row.append($("<td>" + rowData.firstName + "</td>"));
    row.append($("<td>" + rowData.lastName + "</td>"));
}

Any assistance to be pointed in the correct direction would be greatly appreciated thanks.

1 Answer 1

0

Try This-

$result =$con->query("SELECT * FROM tableName"); 
$array = $result ->fetch_all(MYSQLI_ASSOC);          
foreach($array as  $array)
{
   $array['example'];
    //another         
 }           
Sign up to request clarification or add additional context in comments.

1 Comment

Would mysqli syntax work with my mysql script? Is it possible u could suggest where i went wrong to begin with so i can understand it further?

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.