1

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;    
  • so what do u want? the mysqli_query returns an object - see the docs: http://php.net/manual/en/mysqli.query.php – myxaxa Oct 02 '18 at 11:54
  • Maybe I need to do $stmt=$stmt->execute(); this equals 1 now which also does not make sense though... –  Oct 02 '18 at 11:54
  • Please __understand__ the difference between executing a statement and running a `mysqli_query`. – u_mulder Oct 02 '18 at 11:55
  • I want the query to return the row within the database. not "object". –  Oct 02 '18 at 11:56
  • this similar code works when using it without the datatables library... but I will double check.... –  Oct 02 '18 at 11:57
  • `uncaught error: call to a member function fetch_assoc() on null` is your real problem. If `$result` is `null`, you need to know what the mysqli error is – Chris Lear Oct 02 '18 at 11:58
  • hi chris, yes that is why I am print_r ($stmt) because that is the parameter for $result. –  Oct 02 '18 at 11:59
  • You are binding the `edit_id` to `"i"` but using unnamed placeholders in your query. I think this should be `1` instead of `"i"` in `bind_param`. – Sven Koschnicke Oct 02 '18 at 12:21

2 Answers2

0

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.

-1

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;
Sigma
  • 387
  • 3
  • 17
  • ok. this makes more sense to me than the comments. I was only print_r temporarily to find out why $result is null most likely from the second parameter $stmt. let me give it a try –  Oct 02 '18 at 11:59
  • problem I see is that $result is null the whole ajax setup (like echo at the end) would not matter because I have to fix that issue first. –  Oct 02 '18 at 12:00
  • ok so you are probably right the print_r is messing up the ajax. now I get fetch_assoc() on bolean. I will play around with this more. –  Oct 02 '18 at 12:03
  • not sure who downvoted you... this is by far on the right track... marked as answer. –  Oct 02 '18 at 12:06
  • Happy to help, feel free to ask for what is not so clear to you @Jeff. No problem for downvote, the votes is not my life... important is to be useful and to help. – Sigma Oct 02 '18 at 12:06
  • yes I am using datatables.net library so I will have to play around with this some as it already does the row outputs. –  Oct 02 '18 at 12:07
  • 1
    looks like var_dump($result) is the correct way to do this when using ajax. not print_r($result); that returned false which makes sense... –  Oct 02 '18 at 12:10
  • Yes is better to use var_dump so you can understand better what really is returning and how is structured the data. Print_r is usually used to write some data into the client side (html file in general) and whe you use datatables the print_r broke the result that DT is expecting. – Sigma Oct 02 '18 at 12:13
  • Getting an error: Call to undefined method mysqli_result::fetch_all() which is similiar to this article: https://stackoverflow.com/questions/6694437/mysqli-fetch-all-not-a-valid-function......think I think I need to do something like this: `while ($row = $result->fetch_assoc()) { // do what you need. }` However, it still does not work. –  Oct 07 '18 at 23:13