0

Iwas wondering if someone could help me?

I have a table called markers, in this table it stores multiple records each with a name etc. I would like to echo each name however the below code only shows one results. How can I show more than one. Can someone please help I am new to PDO.

$stmt = $dtb->query('SELECT * FROM markers'); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $markerName = $row['name']; } 

3 Answers 3

2

Use an array to hold the result, in your code, the variable $markerName is overwrote on each iteration.

$stmt = $dtb->query('SELECT * FROM markers'); $markerName = array(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $markerName[] = $row['name']; } 
Sign up to request clarification or add additional context in comments.

Comments

0

That is because you are overwriting it each and every time , use an array instead.

Rewrite like this...

$markerName = array(); //<---- Add here while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $markerName[] = $row['name']; } echo implode('<br>',$markerName); //<---- Implode it up for display 

8 Comments

@user3531072, Welcome , you could also give a shot on what YourCommonSense suggested.
I can take a wild guess who -1 you Shankar, but I'll fix that ;-) Right as usual. Plus, giving a good explanation ;-)
@Fred-ii-, Thank you mate :D and I saw the downvotes on your answers and levelled it up too :D
@Fred-ii-, Well- said mate ! :)
Hehehe believe me, I know what I'm talking about ;-)
|
0

Rewrite like this

$names = $dtb->query('SELECT * FROM markers')->fetchAll(); 

Being new to PDO, you are supposed to try tag wiki first, where you can find an answer not only to this one but to many other questions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.