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

304

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];
      $.ajax({
        type:'POST',
        url: 'form.php',
        datatype: "json",
        data: { edit_id : edit_id },
        success: function(result) {
          //alert(edit_id);
          alert(result);
          $("#edit_id").val(edit_id);
        } //success func
      }); //ajax
    }); //end ready
    </script>

Form.php:

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

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

Answer

Solution:

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.

587

Answer

Solution:

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']);
$stmt->execute();
// 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

Source

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.