5

I am receiving a fatal error in my php/mysqli code which states that on line 46:

Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in ...

I just want to know how can I remove this fatal error?

The line of code it is pointing at is here:

$row = $stmt->fetch_assoc();

ORIGINAL CODE:

$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbUser, $dbEmail);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();                                      

if ($numrows == 1){

$row = $stmt->fetch_assoc();
$dbemail = $row['Email'];

}

UPDATED CODE:

$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbUser, $dbEmail);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();                                      

if ($numrows == 1){    
  $row = $stmt->fetch_assoc();
  $dbemail = $row['Email'];    
}
user1633219
  • 45
  • 1
  • 1
  • 5
  • your error means there is no method called fetch_assoc defined in mysqli_stmt class. method name must be different then what you have specified here in that class. – Rukmi Patel Aug 29 '12 at 12:52
  • I beleive it should be $result = $stmt->execute(); $result->fetch_assoc(); – Matt Humphrey Aug 29 '12 at 12:53
  • Perhaps you should talk with your fellow classmates about this, because one of them seemed to have a similar issue a week ago on this same assignment: http://stackoverflow.com/questions/12101053/i-am-getting-errors-and-warnings-in-mysqli – Brad Larson Aug 29 '12 at 14:23

5 Answers5

10

The variable $stmt is of type mysqli_stmt, not mysqli_result. The mysqli_stmt class doesn't have a method "fetch_assoc()" defined for it.

You can get a mysqli_result object from your mysqli_stmt object by calling its get_result() method. For this you need the mysqlInd driver installed!

$result = $stmt->get_result();
row = $result->fetch_assoc();

If you don't have the driver installed you can fetch your results like this:

$stmt->bind_result($dbUser, $dbEmail);
while ($stmt->fetch()) {
    printf("%s %s\n", $dbUser, $dbEmail);
}

So your code should become:

$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute(); 
// bind variables to result
$stmt->bind_result($dbUser, $dbEmail);
//fetch the first result row, this pumps the result values in the bound variables
if($stmt->fetch()){
    echo 'result is ' . dbEmail;
}
Asciiom
  • 9,867
  • 7
  • 38
  • 57
  • `Fatal error: Call to undefined method mysqli_stmt::get_result() in on line 42` It gives me this error pointing to `$result = $stmt->get_result();` if I include your code – user1633219 Aug 29 '12 at 13:04
  • Change that line to $result = $stmt->store_result(); and try again? – Asciiom Aug 29 '12 at 13:05
  • Haha, typical, if I change it to `$result = $stmt->store_result();` I get this error: `Fatal error: Call to a member function fetch_assoc() on a non-object `. – user1633219 Aug 29 '12 at 13:08
  • change back to get_result and try to add this just after your $query=... line: $stmt = $mysqli->stmt_init(); – Asciiom Aug 29 '12 at 13:10
  • It is giving me same error as error mentioned in first comment – user1633219 Aug 29 '12 at 13:40
  • I updated my answer, to use get_result you need the mysqlInd driver installed, if you don't have it, you need to use bind_result and fetch – Asciiom Aug 29 '12 at 13:49
  • I updated my code in the question, I have to use the bind_result() method, do you think the updated code would work now or do I still need to make a change? – user1633219 Aug 29 '12 at 14:01
  • Look closer at my code, do it exactly like that. $smtp->fetch() loads the result values in the variables you bound to the result using bind_result. You don't need to work with these methods' return values. It should work as I posted it – Asciiom Aug 29 '12 at 14:02
  • Also, you shouldn't update your code in the question, now future users with similar problems can't see the original code – Asciiom Aug 29 '12 at 14:04
  • Ok I will get the original code back and put updated code underneath – user1633219 Aug 29 '12 at 14:06
  • Ok that's much better, but try it first to see if it works :) – Asciiom Aug 29 '12 at 14:07
  • OK I don't know who editted the updated code in the question but if I use that one which includes the fetch_assoc(), then I get the fatal error, if I use the fetch(), then I get no errors at all. So am I better off using the fetch() rather than the fetch_assoc()? What is the difference between the two? – user1633219 Aug 29 '12 at 14:16
  • You need to use fetch(), fetch works with the bound variables. fetch_assoc won't work in your case. The code was reverted to the original post by an admin, on my advice. – Asciiom Aug 29 '12 at 14:18
0

Change,

$stmt->store_result();

to

$result = $stmt->store_result();

And

Change,

$row = $stmt->fetch_assoc();

to

$row = $result->fetch_assoc();
Amar Gharat
  • 134
  • 3
0

You have missed this step

$stmt = $mysqli->prepare("SELECT id, label FROM test WHERE id = 1");
$stmt->execute();
$res = $stmt->get_result(); // you have missed this step
$row = $res->fetch_assoc();
Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36
0

I realized that this code was provided as an answer somewhere on stackoverflow:

//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();

I tried it to get the number of rows but realized that i didnt need the line $stmt->store_result();, and it didn't get me my number. I used this:

$result      = $stmt->get_result();
$num_of_rows = $result->num_rows;
......
$row         = $result->fetch_assoc();
$sample      = $row['sample'];
nyxee
  • 2,773
  • 26
  • 22
0

It's best to use mysqlnd as Asciiom pointed out. But if you're in a weird situation where you are not allowed to install mysqlnd, it is still possible to get your data into an associative array without it. Try using the code in this answer Mysqli - Bind results to an Array

Dan Cron
  • 1,105
  • 12
  • 24