0

I am facing something really strange and cant understand why I am getting this next result, I have 2 files, index.php and send.php send.php fetching from the DB list of users and index.php showing the list as table, in index.php I am using require send.php, in send.php i have this code

    $sth->setFetchMode(PDO::FETCH_ASSOC);
    $sth->execute();
    $result = $sth->fetchAll();

    echo '<pre>';
    print_r($result);

and its working great, but in index.php

    <?php if (isset($result)): ?>
        <pre>
            <?php print_($result); ?>
        </pre>
    <?php endif; ?>

I am getting the array like this [0] => array

why?

6
  • 1
    print_() will not work. typo Commented Mar 26, 2017 at 11:51
  • Is any of that code inside a function or method by any chance Commented Mar 26, 2017 at 11:51
  • @Yolo I am guessing that is a typo in the question only otherwise OP would not get the output they suggest they are getting Commented Mar 26, 2017 at 11:52
  • Showing us more of the real code would probably get you a useful answer. As it stands we would just have to make guesses! Commented Mar 26, 2017 at 11:56
  • its really simple code, and about the typo its only in this post, and no its not in function or method, I continue to work in different method but its really wired i dont understand why its acting like this. Commented Mar 26, 2017 at 12:46

1 Answer 1

1

Should be obvious, unlike fetch() where you iterate over each row fetchAll() returns a list of rows, each being in a separate index of the array.

$result[0]['column'];
        ^
        |-- first result.

So you should do this:

foreach($result as $row){
  echo $row['colname'];
}

This would make $row an array by default, where the key is the column name.


On a side note, if you want to check if you have results do the following:

// this:
if($result){}

// equals:
if(!empty($result)){}

Using isset() will cause unexpected behavior:

if(isset($result = false)){
  // query failed, but this code still executes.
}

Also trying to access a global variable defined in another script is bad practice, avoid doing it. Wrap your code in a small function instead:

function dbGetUsers(){
  // connect to db or whatnot.
  $sth = $pdo->prepare('....');

  if($sth->execute()){
    return $sth->fetchAll();
  } else {
    die('query failed '. __FILE__ . __LINE__ - 2);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think you didnt understand me, then i am using print_r on the fetchAll variable i am getting the data correctly, but then i am using print_r in index.php (in index.php i am using require to the second page) an i am getting the reult of [0] => array, and the array is a string and not array itself, later i noticed that when i am trying to send the result via curl i am getting again the wired array.
Then somewhere in the line you are overwriting the variable $result, globals shouldn't be used like that anyways and you should use a function instead.
i double checked the $result, and its the only variable that i named it like that, and even the lenth of the array is the same just the value different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.