rick.emmet Posted January 25, 2012 Share Posted January 25, 2012 Hi Everyone, 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: <?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: $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 Quote Link to comment https://forums.phpfreaks.com/topic/255781-mysqli_stmt_fetch-couldnt-fetch-mysqli_stmt/ Share on other sites More sharing options...
jcbones Posted January 26, 2012 Share Posted January 26, 2012 Have you tried using mysqli_stmt_error? This will tell you exactly what went wrong. Quote Link to comment https://forums.phpfreaks.com/topic/255781-mysqli_stmt_fetch-couldnt-fetch-mysqli_stmt/#findComment-1311284 Share on other sites More sharing options...
rick.emmet Posted January 26, 2012 Author Share Posted January 26, 2012 Hi jcbones, Thanks for getting back to me! Yes I have tried to use that - 'echo "Error message is".mysqli_stmt_error(stmt);' and nothing but the text I wrote prints out on the page. I believe that the only thing coming back from mysqli_stmt_fetch($stmt) is a boolean for false. Cheers, Rick Quote Link to comment https://forums.phpfreaks.com/topic/255781-mysqli_stmt_fetch-couldnt-fetch-mysqli_stmt/#findComment-1311435 Share on other sites More sharing options...
scootstah Posted January 26, 2012 Share Posted January 26, 2012 Try echo mysqli_error($conn); Quote Link to comment https://forums.phpfreaks.com/topic/255781-mysqli_stmt_fetch-couldnt-fetch-mysqli_stmt/#findComment-1311453 Share on other sites More sharing options...
rick.emmet Posted January 27, 2012 Author Share Posted January 27, 2012 Hi Scootstah, Thanks for replying. I tried that too and get the same result, nothing but the message I wrote into the code is printed on the page. It seems so simple, I wonder how this could even be occurring. I'll keep working on it. Cheers, Rick Quote Link to comment https://forums.phpfreaks.com/topic/255781-mysqli_stmt_fetch-couldnt-fetch-mysqli_stmt/#findComment-1311769 Share on other sites More sharing options...
jcbones Posted January 28, 2012 Share Posted January 28, 2012 Where is mysqli_stmt_init? Quote Link to comment https://forums.phpfreaks.com/topic/255781-mysqli_stmt_fetch-couldnt-fetch-mysqli_stmt/#findComment-1311946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.