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?
echo $result[0];returns the first character of the scalar$result, as you are using->fetchColumnto only fetch 1 column, not the whole row. So, eitherecho $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.$result = $stmt->fetch(PDO::FETCH_ASSOC); if($result){echo $result['id'];}should work.$resultis going to either be boolean FALSE or an array so a simple check will avoid undesireable errorsSELECT 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.