gpt4 book ai didi

php - 显示函数中的数组内容?

转载 作者:行者123 更新时间:2023-11-29 06:15:39 27 4
gpt4 key购买 nike

谁能告诉我如何显示下面代码中的 next 值?

在我的 user.php 文件中,我有以下内容:

class User {
protected $userID;
protected $useremail;
protected $userPassword;
public function __construct() {
$this->userID = preg_replace('#[^0-9]#i', '',
$_SESSION['user_id']);
$this->useremail = preg_replace('#[^A-Za-z0-9@_.-]#i', '',
$_SESSION['user']);
$this->userPassword = preg_replace('#[^A-Za-z0-9]#i', '',
$_SESSION['user_password']);
}

public function UserInfoQuery() {
$sql = "SELECT * FROM users WHERE id =
'$this->userID' AND email = '$this->useremail' AND
password = '$this->userPassword' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
$userMatch = mysql_numrows($res);

if ($userMatch == 1) {
while($row = mysql_fetch_array($res)) {
$userData = array(
$userFirstname = $row['firstName'],
$userLastname = $row['lastName'],
$userBirthdate = $row['birthDate'],
$userSex = $row['sex'],
$userEmail = $row['email'],
$userCountry = $row['country'],
$userRegion = $row['region']);
}
}

return $userData;
}
}

当我尝试时,在我的索引 php 文件中:

$User = new User();
print_r($User->UserInfoQuery());

我有下一个结果:

Array ( [0] => firstname [1] => 
lastname [2] =>
1990-11-23 [3] =>
male [4] =>
mail [5] =>
Srbija [6] => town )

如何仅回显名字和姓氏?

最佳答案

这个:

array($userFirstname = $row['firstName'])

$row['firstName'] 的值赋给变量 $userFirstname,然后将赋值结果($row 的值['firstName']) 到一个数组中。与编写相同:

$userFirstname = $row['firstName'];
array($row['firstName']);

要使用 key userFirstname 声明一个数组,您需要编写:

array('userFirstname' => $row['firstName'])

从这里,您可以访问一个普通数组:

$userinfo = $User->UserInfoQuery();
echo $userinfo['userFirstname'];

这看起来确实有点笨拙,而且说实话,你在这里并没有很好地使用对象。您应该将从数据库查询到的数据保存到对象的属性中,然后使用 getter 来一一或全部访问这些属性。不过,如何设计一个合适的对象有点超出了这个答案的范围/要点。

关于php - 显示函数中的数组内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6769700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com