Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

php - Fatal error: Call to a member function fetchAll() on a non-object in pdo

I'm new to pdo just tried the following and getting fatal error.

$pdo = new pdo('mysql:localhost;widget_corp;charset=utf-8', 'root', '');
$query = $pdo->query("SELECT * FROM 'users'");
$result_array = $query->fetchAll(PDO::FETCH_ASSOC);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change:

mysql:localhost;widet_corp

to

mysql:host=localhost;dbname=widget_corp

Also in your posted code:

$query = $pdo->query("SELECT * FROM 'users'");

you have the table name inside single quotes. In Mysql you should use a backtick instead for tables and columns.

    $query = $pdo->query("SELECT * FROM `users`");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...