php - why would $stmt->execute() print_r = Object in ajax


Intro: I am doing a serverside datatables library using ajax.

Error: It looks like my print_r($stmt) is returning Object. This first error I believe is causing my other errors such as "uncaught error: call to a member function fetch_assoc() on null".

Additionally, I do not think print_r($result) is showing any details.

Ajax Code:

 <script type="text/javascript"> 
    $(document).on('click','.edit_btn',function (){
      var id = $(this).attr("id").match(/\d+/)[0];
      var edit_id = $('#example').DataTable().row( id ).data();
      var edit_id = edit_id[0];
        url: 'form.php',
        datatype: "json",
        data: { edit_id : edit_id },
        success: function(result) {
        } //success func
      }); //ajax
    }); //end ready


$stmt = $conn->prepare("SELECT * FROM `employees` WHERE `id` = ?");
$stmt->bind_param("i", $_POST['edit_id']);
$result = mysqli_query($conn, $stmt);

$row = $result->fetch_assoc();
echo $row;    



In simplest terms you cannot use print_r in Ajax as it conflicts with the echo statement at the end. Instead, You must use var_dump when checking variables.




On the php file you are printing with print_r and this gives errors when use with DT. First comment the print_r lines and in second i cant see a while loop to insert the returned data of the query into an array and after this you must encode the final echo.

As i dont know the info from your table employees lets do an example: Your query should be like:

$stmt = $conn->prepare("SELECT * FROM `employees` WHERE `id` = ?");
$stmt->bind_param("i", $_POST['edit_id']);
// here is the change part
$result = $stm->fetchAll(); // save all returned values into $result as an object
$output = ''; // declare a variable to handle the data
// loop through all returned rows from query
foreach($result as $rows) {
    $output .= '<td>'.$rows["user_name"].'</td>'
    $output .= '<td>'.$rows["user_surname"].'</td>'
    $output .= '<td>'.$rows["user_email"].'</td>'
    $output .= '<td>'.$rows["user_phone_nr"].'</td>'
    ..... // and so on with the values you want to show (i just insert into a <td> which is supposed to be part of a table declared before the loop)

echo $output;

People are also looking for solutions to the problem: Backup whole mysql database with php to the specific file/location


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.