1
$sql = "SELECT name FROM people WHERE id = '$id'";  
$array = array();       
$q = $pdo->prepare($sql);
$q->execute();
foreach ($pdo->query($sql) as $row) {
$array[] = $row;
}

My result is:

array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(4) "fred"
    [0]=>
    string(4) "fred"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(3) "sam"
    [0]=>
    string(3) "sam"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(4) "alan"
    [0]=>
    string(4) "alan"
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(63) "john"
    [0]=>
    string(63) "john"
  }
}

But I would need:

Array
(
    [fred] => fred
    [sam] => sam
    [alan] => alan
    [john] => john
)

I tried for example:

foreach ($pdo->query($sql) as $key => $row) {..

and also

foreach ($pdo->query($sql) as $row) {
$array[] = $row;
$row = array_combine(array_values($row), array_values($row));
}

but I always get the same result...

0

3 Answers 3

4

Use fetchAll

$q = $pdo->prepare($sql);
$q->execute();
$result = $q->fetchAll();
print_r($result);
Sign up to request clarification or add additional context in comments.

6 Comments

Also you have to specify fetch mode.
Thank you! :) Does this method has some advantages to Ashwanis suggestion, or is it up to personal taste which method to choose
This in in build function of PDO you can direct get your desier result without use of any loop
@fusion3k in that case no use of any mode
fetchAll() in itself employs a loop.
|
1

Use fetchAll()

But not a vanilla one but with a secret argument called PDO::FETCH_COLUMN

$sql = "SELECT name FROM people WHERE id = ?";  
$q = $pdo->prepare($sql);
$q->execute([$id]);
$array = $q->fetchAll(PDO::FETCH_COLUMN);

It will get you a single-dimensional array with names like this

Array
(
    [0] => fred
    [1] => sam
    [2] => alan
    [3] => john
) 

Note that you are using prepared statements completely wrong way making your code vulnerable to SQL injection. It is fixed in the code above.

If you insists on getting both key and value, then change your code as follows

$sql = "SELECT name, name FROM people WHERE id = ?";  
$q = $pdo->prepare($sql);
$q->execute([$id]);
$array = $q->fetchAll(PDO::FETCH_KEY_PAIR);

and it will give you exact array as you asked

3 Comments

Out of curiosuty, which one you choose? By the way, there are other magic modes in PDO, worth a quick glance. May be I should make a cheatsheet...
I was choosing the second solution. Could you tell me why my statements are insecure?
Because you are adding variables directly to the query, which makes it vulnerable to SQL injection.
0

Make the following changes:

foreach ($pdo->query($sql) as $row) {
    $array[$row['name']] = $row['name'];
}

3 Comments

Great! Working very well :o)
I am glad that it helped, I would seek your approval/acceptance to my answer :-)
@Jarla please approve the answer so that people can rely over it.

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.