[英]PHP Bind_param fetch data
Guys im using Bind_param in php to retrieve Username and password from Login table. 伙计们我在php中使用Bind_param从登录表中检索用户名和密码。 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() . 好像你正在寻找的是提取物() ,与使用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: 这样,您可能希望将要提取的行包装在array_intersect_key()中 ,如下所示:
$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. 因此您的代码记录了哪些字段会突然变成(局部或全局)变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.