Reputation: 85
I'm trying to get a list from my MySQL database, which normally works fine. But today I'm getting this error Fatal error: Call to a member function setFetchMode() on a non-object in "Way too long path to file here"
.
This is my PHP code:
$conn = new PDO('mysql:host=localhost;port=3306;name=erty', 'erty', 'Ops, that my password ...');
$result = $conn->query("SELECT name FROM mod_devs");
$result->setFetchMode();
foreach ($result as $row) {
echo '<tr><td>'.$row['name'].'</td></tr>';
}
Now, you probably understand my goal :)
Upvotes: 1
Views: 71
Reputation: 85
I just found the error. In the connection I wrote name=erty
not dbname=erty
!
Sorry for taking up your time :(
Upvotes: 0
Reputation: 37233
you maybe need prepare not query
$result = $conn->prepare("SELECT name FROM mod_devs");
or this
$result->setFetchMode(PDO::FETCH_OBJ);
Upvotes: 0
Reputation: 11749
You still need to fetch the result... So instead of foreach....do while...
while ($row = $result->fetch()) {
//your code here
}
Upvotes: 1
Reputation: 12036
Generally Call to a member function setFetchMode() on a non-object
means that $result is not available. Probably because of MySQL error - either in connection or query. Check if($conn)
or if($result)
.
Upvotes: 1