How to store a single column of a single row out of a MySQLi prepared statement in a PHP variable? -


i'm new php , mysql , i'm looking solution store single value of database row in variable using prepared statement.

right prepared statement , execution:

$emailsql = $conn->prepare("select email user email = ? limit 1;"); $emailsql->bind_param('s', $email); $emailsql->execute(); 

i tried get_result(), fetch(), fetch_object() , i'm out of ideas , google search results.

you need add code binding of result specific variable

$emailsql->bind_result($emailresult);   

and fetch :

while($emailsql->fetch()){      printf ($emailresult);  } 

so should it:

$emailsql = $conn->prepare("select email user email = ? limit 1;"); $emailsql->bind_param('s', $email); $emailsql->execute(); $emailsql->bind_result($emailresult);  while($emailsql->fetch()){      printf ($emailresult);  }  

in case need variable outside loop take approach:

$theemail; $emailsql = $conn->prepare("select email user email = ? limit 1;"); $emailsql->bind_param('s', $email); $emailsql->execute(); $emailsql->bind_result($emailresult);  while($emailsql->fetch()){      $theemail=$emailresult;  }   

note need array in order query more 1 email.

another cleaner approach @yourcommonsense suggested avoiding loop so:

$theemail; $emailsql = $conn->prepare("select email user email = ? limit 1;"); $emailsql->bind_param('s', $email); $emailsql->execute(); $emailsql->bind_result($emailresult);  $emailsql->fetch();    printf($emailresult); 

hope helps you


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -