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
Post a Comment