Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

MySQLi Code Not working

Codes here !

Moderators: macek, egami, gesf

MySQLi Code Not working

Postby eastland » Tue Feb 18, 2014 5:29 pm

I'm trying to write a procedural style mysqli query with prepared statements that is called from a mobile app.

Code: Select all
$sql = "SELECT id, first_name, last_name, group_id, email, cell
            FROM Users
            WHERE (first_name=? && last_name=?)";
$stmt =  mysqli_stmt_init($link);
   
if (mysqli_stmt_prepare($stmt, $sql)) {

    /* Bind the input parameters to the query */
    mysqli_stmt_bind_param($stmt, 'ss', $first_name, $last_name);

    /* execute query, bind result, and fetch value */
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $id, $first_name, $last_name,  $group_id, $email, $cell);
    mysqli_stmt_fetch($stmt);
 
    if (mysqli_stmt_num_rows($stmt) == 0) {
        mysqli_stmt_close($stmt);
        mysqli_close($link);
        echo "No results returned";
        die();
    }
   
    ...
}

This doesn't return any errors, but it always returns "No results returned" even though I know that the parameters I am feeding it should return exactly 1 record. Can anyone help me fix this? Thanks!
eastland
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Feb 18, 2014 5:20 pm

Re: MySQLi Code Not working

Postby haerethere » Thu Feb 20, 2014 1:24 am

Hi. Try to check for result variables $group_id, $email, etc for value.
I don't think "mysqli_stmt_num_rows($stmt)" is the right way to check for result here, althought i might be wrong.
haerethere
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Feb 20, 2014 1:22 am

Re: MySQLi Code Not working

Postby eastland » Thu Feb 20, 2014 7:53 am

Thanks for the reply. All of that stuff is fine. I can run this query if I don't use prepared statements, so I know that everything is working fine up to the point where I execute the query. Somewhere in there, however, is where I am going wrong, but I can't figure it out.
eastland
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Feb 18, 2014 5:20 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron