0

The following code is giving me this error:

Call to undefined method mysqli_stmt::get_result() on line 21.

I don't understand how this object works and why I can do the first database call but not the second.

    <?php 

    header('Content-Type: application/json');
    include_once 'do_dbConnect.php';
    include_once 'functions.php';
    sec_session_start(); 

    //identify who took the last call
    $stmt =  $mysqli->stmt_init();
    if ($stmt->prepare("SELECT MAX(dateOfCall), id FROM call")) { //setup the query statement
      $stmt->execute(); //execute the statement
      $result = $stmt->get_result(); //get the results
      $row = $result->fetch_assoc(); //get the first row
      $user_id = $row['id']; //get the id column
    }


    //identify how many team members there are
    if ($stmt->prepare("SELECT id FROM teamMembers")) { //setup the query statement
      $stmt->execute(); //execute the statement
      $result = $stmt->get_result(); //get the results
      $memberCount = $result->num_rows;
    }


    //get next user
    if ($stmt = $mysqli->prepare("SELECT * FROM teamMembers WHERE id = (? + 1) % ?")) { //setup the query statement
      $stmt->bind_param('ii', $user_id, $memberCount);
      $stmt->execute(); //execute the statement
      $result = $stmt->get_result(); //get the results
      $row = $result->fetch_assoc(); //get the first row
      $next_user_id = $row['id']; //get the id column
      $next_user_name = $row['username'];
    }
    $stmt->close();
    //get the next call taker from the teamMember table

    echo json_encode($row);

    ?> 
Félix Gagnon-Grenier
  • 7,344
  • 10
  • 45
  • 58
b1shp
  • 35
  • 4
  • 1
    please specify which line is 21?? how do you know the first mysqli call is ok? – Félix Gagnon-Grenier Jul 25 '14 at 19:34
  • I don't know the answer to your problem but I always find that understand how something works is often the most important step. See this comment: http://stackoverflow.com/a/8343970/1888402 Also, don't be shy, it's a public forum, people love to get points for helping people out so it's a win win for everyone involved. – ZeroBased_IX Jul 25 '14 at 19:34
  • 1
    One problem is with your first query **call** is a [*Mysql reserved keyword*](http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html) you need to escape it with bact-ticks – M Khalid Junaid Jul 25 '14 at 19:35
  • possible duplicate of [mySQLi prepared statement unable to get\_result()](http://stackoverflow.com/questions/10466530/mysqli-prepared-statement-unable-to-get-result) – Machavity Jul 25 '14 at 19:36
  • As stated, `call` is a reserved word. Use backticks `\`` or rename it to `calls`. May very well fix the problem. – Funk Forty Niner Jul 25 '14 at 19:37
  • Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Jul 25 '14 at 19:53
  • *"I don't understand how this object works and why I can do the first database call but not the second."* - I doubt that very much. – Funk Forty Niner Jul 25 '14 at 19:54
  • I don't use mysqli myself, but may it be that you need to re-init $stmt before each query? Just a thought... – Niklas Jul 25 '14 at 19:55
  • Okay, changed call to calls - thanks for that. – b1shp Jul 25 '14 at 19:59
  • Now error is on line 12 - $result = $stmt->get_result(); //get the results Call to undefined method mysqli_stmt::get_result() – b1shp Jul 25 '14 at 20:00
  • Just in case it is relevant my php is PHP Version 5.3.28 – b1shp Jul 25 '14 at 20:11
  • I would check and make sure your very first query isn't causing an sql error first, as I recall, a statement that fails gets put into an error state and either can't be used any more or can't be used until you do something special to restore its status, although I do not know what it is. – Lochemage Jul 25 '14 at 20:15
  • Okay, @Jezzabeanz your suggestion about the getresult method and the mysqlnd driver seem to be the problem as switching to bind and fetch has worked. Thanks everyone for the help. Jezzabeanz, if you post that as the solution I'll accept it happily! Thanks again. – b1shp Jul 28 '14 at 13:31

1 Answers1

0

Please read the user notes for this method:

http://php.net/manual/en/mysqli-stmt.get-result.php

It requires the mysqlnd driver... if it isn't installed on your webspace you will have to work with BIND_RESULT & FETCH!

http://www.php.net/manual/en/mysqli-stmt.bind-result.php

http://www.php.net/manual/en/mysqli-stmt.fetch.php

ZeroBased_IX
  • 2,468
  • 2
  • 22
  • 42