1

I have some data in a mysql database.The database every time consists of a different number of rows.

An example of my table would be :

id  team1  team2  home  away  tip
----------------------------------
12   xxx    yyy   zzz   kkk
43   xxx    yyy   zzz   kkk
.     .      .     .     .
.     .      .     .     .

So what i need to do is loop through every row of the table and be able to get the data from that row so i can create the tip.

I am a bit confused on how i could do that with pdo.

What i ve tried so far is try to get a column of data like this:

$stmt = $db->prepare("SELECT id FROM tablename");
    $stmt->execute();
    $result=$stmt->fetchColumn();
    echo $result[0];

But even that , that i would expect to give me back the first id , which is 12 in this case, just returns me 1. Any ideas here?

4
  • 3
    echo $result[0]; returns the first character of the scalar $result, as you are using ->fetchColumn to only fetch 1 column, not the whole row. So, either echo $result, or use $result=$stmt->fetch();. Currently, what's happening can be compared to this: $result="12"; echo $result[0]; (which, as you can test, echoes '1' indeed. Commented Feb 18, 2014 at 14:49
  • $result = $stmt->fetch(PDO::FETCH_ASSOC); if($result){echo $result['id'];} should work. $result is going to either be boolean FALSE or an array so a simple check will avoid undesireable errors Commented Feb 18, 2014 at 14:50
  • @wrikken, when i use $result=$stmt->fetch(); does indeed work. but when i try to echo $result[1] gives me an error. How can i get all the column in an array so i can loop through the results? Commented Feb 18, 2014 at 14:54
  • If you SELECT id FROM tablename, you only have one column, so, $result[1] will not be set indeed. Alter your query to return the other columns, and they'll appear in $result. Commented Feb 18, 2014 at 14:58

5 Answers 5

2
$stmt = $db->prepare("SELECT id FROM tablename");
try{
    $stmt->execute();
}catch(PDOException $err){
    //some logging function
}
//loop through each row
while($result=$stmt->fetch(PDO::FETCH_ASSOC)){
    //select column by key and use
    echo $result['id'];
} 

alternatively you could use fetchAll to select whole dataset into a variable. Then you wouldn't need to loop

Heres a useful resource for PDO:

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

Sign up to request clarification or add additional context in comments.

1 Comment

This is relevant to me
0
$getUsers = $DBH->prepare("SELECT * FROM tablename ORDER BY id ASC");
$getUsers->execute();
$users = $getUsers->fetchAll();

May be helps you

Comments

0

Try to print the result without index

$stmt = $db->prepare("SELECT id FROM tablename ORDER BY id ASC");
$stmt->execute();
$result = $stmt->fetchColumn();
echo $result;

Comments

0

Use fetch (with FETCH_NUM) instead of fetchColumn

$stmt = $db->prepare("SELECT id FROM tablename");
    $stmt->execute();
    $result=$stmt->fetch(PDO::FETCH_NUM);
    echo $result[0];

Comments

0

if you want to loop throught you can use the while loop and below is the example

<?php 

$stmt =("SELECT id FROM tablename");
$result=$db->prepare($stmt );
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$id = $row['id'];
 echo $id;
}
?>

Comments

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.