简体   繁体   中英

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:

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']; but that returned an error 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

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

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.

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).

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.

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM