Guys im using Bind_param in php to retrieve Username and password from Login table. my question is how can i fetch all the info of user, and pass it to variable as an object? please see my code below
require 'dbc.php';
require 'security.php';
$myusername=trim($_POST['strusername']);
$mypassword=trim($_POST['strpassword']);
$myusername =escape($myusername);
$mypassword=escape($mypassword);
$sql = "SELECT * FROM login WHERE strusername=? AND strpassword=?";
$stmt = $db->prepare($sql);
$stmt->bind_param('ss',$myusername,$mypassword);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows){
echo "user verified, Access Granted.";
// while($row=$stmt->fetch_object()){
// $strfullname= $row->strfullname;
// $strcompany= $row->strcompany;
// $strdept= $row->strdept;
// $strloc= $row->strloc;
// $strposition= $row->strposition;
// $strauthorization= $row->strauthorization;
// $stremailadd= $row->stremailadd;
// $strcostcent= $row->strcostcent;
// $strtelephone= $row->strtelephone;
// };
// how to fetch all data in my query using param LIKE THIS
}else
{
echo "Invalid Username or Password";
}
Seems like what you're looking for is extract() , used with fetch_row() . So you'd end up with something like:
while ($row = $stmt->fetch_row()) {
extract($row, EXTR_OVERWRITE);
// your logic here...
}
As fair warning, this will overwrite any variables with the same name as your database columns. Since you're pulling from the database rather than a superglobal you at least know what you're getting into, but it's still a risk, particularly if you've got this code in global scope rather than inside a function/method.
As such, you may want to wrap your row-to-be-extracted in array_intersect_key() , like so:
$allowedFields = array_flip(['strfullname', 'strcompany', 'strdept',
'strloc', 'strposition', 'strauthorization', 'stremailadd',
'strcostcent', 'strtelephone']);
while ($row = $stmt->fetch_row()) {
extract(array_intersect_key($row, $allowedFields), EXTR_OVERWRITE);
// your logic here...
}
so your code documents which fields will suddenly turn into (local or global) variables.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.