Jump to content

Recommended Posts

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.