[英]php How to echo array data separately without using foreach
I have created the following function to return member data.我创建了以下函数来返回成员数据。 However, I am getting an error when I am trying to return the data.但是,当我尝试返回数据时出现错误。 Maybe, my method is wrong.也许,我的方法是错误的。 What should be the correct way considering the following details?考虑到以下细节,正确的方法应该是什么?
function memberData(){
global $pdo;
$stmt = $pdo->prepare("SELECT * FROM members WHERE mem_id = :mem");
$stmt-> bindValue(':mem', userId());
$stmt-> execute();
while($f = $stmt->fetch()) {
$return[] = $f;
}
return $return;
}
$memData = memberData();
Doing print_r($memData)
returns array of data as following:执行print_r($memData)
返回数据数组如下:
Array
(
[0] => Array
(
[mem_id] => 1
[0] => 1
[mem_name] => Shubham Jha
[1] => Shubham Jha
[mem_email] => shubhamjha1000@gmail.com
[2] => shubhamjha1000@gmail.com
[mem_phone_ext] => +91
[3] => +91
[mem_phone] => 9876543210
[4] => 9876543210
[mem_password] => $2y$10$x5LfvrM8VVH/Rzx9NlCW/uktETYufS8yH4VPN/mcwvpmlBJcbarDe
[5] => $2y$10$x5LfvrM8VVH/Rzx9NlCW/uktETYufS8yH4VPN/mcwvpmlBJcbarDe
[mem_phone_verified] => yes
[6] => yes
[mem_email_verified] => no
[7] => no
[mem_date] => 2020-11-16 08:33:22
[8] => 2020-11-16 08:33:22
)
)
How do I echo data separately?如何分别回显数据? I tried using echo $memData['mem_id'];
我尝试使用echo $memData['mem_id'];
but that returned an error Notice: Undefined index: mem_id in E:\\xampp\\htdocs\\market\\includes\\functions.php on line 157
但返回了一个错误Notice: Undefined index: mem_id in E:\\xampp\\htdocs\\market\\includes\\functions.php on line 157
You are returning an array from you function (unnecesarity in my opinion) but that will make $memData
an array so use您正在从您的函数返回一个数组(在我看来是$memData
的),但这将使$memData
成为一个数组,因此请使用
echo $memdate[0]['mem_id'];
Alternatively as you are fetching a row on its key and therefore only one will be returned you could do或者,当您在其键上获取一行时,因此只会返回一个,您可以这样做
function memberData(){
global $pdo;
$stmt = $pdo->prepare("SELECT * FROM members WHERE mem_id = :mem");
$stmt-> bindValue(':mem', userId());
$stmt-> execute();
$f = $stmt->fetch();
return $f;
}
$memData = memberData();
echo $memdata['mem_name']; // for example
And it would be much better to inject the $pdo
connection handle to the function rather than using global
like并且最好将$pdo
连接句柄注入函数而不是使用global
像
function memberData($pdo){
$stmt = $pdo->prepare("SELECT * FROM members WHERE mem_id = :mem");
$stmt-> bindValue(':mem', userId());
$stmt-> execute();
$f = $stmt->fetch();
return $f;
}
$memData = memberData($pdo);
echo $memdata['mem_name']; // for example
Becase your have an array, and a array it's a collection of another array's or object, so you need to do echo $memData[0]['mem_id']
as example of your code.因为你有一个数组,一个数组是另一个数组或对象的集合,所以你需要做echo $memData[0]['mem_id']
作为你的代码示例。
I strongly suggest to you study primordial programming and basic of the objects in programming (this is not related to programming in PHP, but general programming).我强烈建议你学习原始编程和编程中对象的基础(这与PHP编程无关,而是通用编程)。
And please, read the documentation , if you get 1 hour everyday to read the documentation it'll be good for you, with a simple search you can found anything you want to learn.并且请阅读文档,如果您每天花 1 小时阅读文档对您有好处,通过简单的搜索,您可以找到任何您想学习的内容。
As to discorver you problem if you ever have read this artice in the official php documentation you'll gonna avoid this kind of errors and problems in the future.至于发现你的问题,如果你曾经在官方的 php 文档中阅读过这篇文章,你将来会避免这种错误和问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.