Jump to content

prepared statement problem


jimfog

Recommended Posts

Take a look at this prepared statement...:

if($stmt=$connection->prepare('SELECT Bookfrom            FROM appointments,users            WHERE users.email=(?)            AND appointments.bookedfor=users.user_ID AND not deleted'))       {            $stmt->bind_param('s',$email);            $stmt->execute();             if($stmt->errno!==0)                 {printf("Error-execution failed-GET appt_failed: %s.n", $stmt->error);                 return false;                 }            $stmt->bind_result($Bookfrom);            $stmt->fetch();            var_dump( $stmt->fetch());            $stmt->close();            return;       }     if(!$result)        {        printf("Errormessage for result: %sn", $connection->error);        return false;        }

I get no error messages.

 

The query is correct...I have checked it many times...it does produce a result/row.

 

Despite the above though....var_dump($stmt->fetch() gives NULL.

I am running out of ideas...

Edited by jimfog
Link to comment
Share on other sites

Is there more than 1 row? You're using fetch twice. It will return null if there are no more rows.

Ι cannot believe it...you are right...there is only one row and it had not crossed my mind that var_dump(stmt->fetch()) was considered a second call to fetch

Link to comment
Share on other sites

I need to stress one more thing...

if there are no rows at all...then fetch() will return NULL.

 

The above check though must be done and if indeed NULL is returned, exit from the function using return....the code will look like something like this:

   if($stmt->fetch()==NULL)          {return;}          else{             while($stmt->fetch())            {                echo $staffID.','.$Bookfrom.','.$startDate;//these refer to data coming from the database            }           }

As you might expect though...the above returns nothing(when we have one row)...cause fetch() is used twice.

 

How I could write the code such a check for NULL is done and at the same time results for 1 row are returned.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...