PHP/MySQL Understanding What is Returned by Prepared Statement -
sorry newbie question - i'm experienced programmer trying head around lamp data retrieval. unfortunately i'm trying ask basic can't find answer!
i'm building simple login script , have following:
$sparklyusername = strtolower($_post['sparklyusername']); $sparklypassword = $_post['sparklypassword']; if($stmt = $sparklydatabaseconnection -> prepare("select username sys_users username=?")) { $stmt -> bind_param("s", $sparklyusername); $stmt -> execute(); /* execute query */ $stmt -> bind_result($result); $stmt -> fetch(); now working fine if try , retrieve more 1 column, example:
if($stmt = $sparklydatabaseconnection -> prepare("select username, password, email sys_users username=?")) { i error "number of bind variables doesn't match number of fields in prepared statement".
now used vbscript query returns recordset (e.g. resultsrs("")) query column name (e.g. resultsrs("username")) specific column can gather, code above bind each column return single variable this:
$stmt -> bind_result($username, $password, $email); if that's case, how check if row has been returned @ all? in vbscript check if recordset empty (e.g. if resultsrs.eof).
and if need loop through records returned, examples i've seen show this:
while ($row = $result->fetch_assoc()) { but if seems create row expect column!
sorry - know i'm missing fundamental difference here between how returned data handled in php , vbscript - waiting fro eureka moment!
thanks in advance , help.
bob
p.s. yes have o'reilly programming php book there's no detail in there!
what returned prepared statement
it's long story. , book writers have not slightest idea on it.
what need know id mysqli prepared statements aren't intended used in application code is, source material higher level abstraction library.
so, if aren't going write one, quit using mysqli, move pdo. has far more intuitive api , familiar ways result.
$sql = "select username, password, email sys_users username=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array($_post['sparklyusername'])); $row = $stmt->fetch(); it indeed create row, columns can address $row['username']
this way have data no matter how many fields requested. , can put fetch() while loop well.
Comments
Post a Comment