Page 1 of 1

mysqli_stmt_fetch() Couldn't fetch mysqli_stmt

Posted: Wed Jan 25, 2012 1:07 pm
by rick.emmet
I've been using prepared statements to insert data into my database and they have been working just fine. I wanted to try prepared statements for select queries and began testing with the code provided at the PHP site. There are a couple of examples in the manual - one for mysqli_prepare() and another for mysqli_stmt_fetch(). The code looks like this:

Code: Select all

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$city = "Amersfoort";

/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name=?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $city);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $district);

    /* fetch value */
    mysqli_stmt_fetch($stmt);

    printf("%s is in district %s\n", $city, $district);

    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
?>
I am testing this code with a database and using SHA1 encryption for passwords. My code is as follows:

Code: Select all

 $username = "somename";
	$passwd = "somepass";
	
	// Check if username is unique
	$stmt = mysqli_prepare($conn, "select verify from users where user_name=? and password=sha1(?)");
	mysqli_stmt_bind_param($stmt, "ss", $username, $passwd);
	mysqli_stmt_execute($stmt);
	mysqli_stmt_bind_result($stmt, $verify);
	mysqli_stmt_fetch($stmt);
		echo "The registration varification is ".$verify."<br />";
		
	// Close the statement
	mysqli_stmt_close($stmt);
		
	// Close the link
	mysqli_close($conn);
The results are not as expected as I get the error message, Warning: mysqli_stmt_fetch() Couldn't fetch mysqli_stmt. I've looked up the error and I haven't found anything on the web that explains what's causing it. I can echo the value of $verify, which I'll need farther down the script, but mysqli_stmt_fetch is returning "false", and I need a return of "true" as a conditional to test the state of a users account (in this case the state of the account should return "true"). I have used the hash version of the password and that yields the same result. Could someone please clue me in? I have no idea what the issue is. Thanks much for your time!
cheers,
Rick

Re: mysqli_stmt_fetch() Couldn't fetch mysqli_stmt

Posted: Thu Jan 26, 2012 10:41 am
by rick.emmet
I should probably note that I have used "mysqli_stmt_error(stmt)" and get no return - it looks as if the only thing that mysqli_stmt_fetch is returning is a boolean for false.
Cheers,
Rick