-3

I was just simply trying to insert UTF8 data in database and retrieve but there is somethings really messing around ! It's just showing up one error.

Here is my table structure and i think there is nothing wrong.

Fatal error: Call to a member function fetchAll() on boolean

Here is source codes. I did search about this problem but these are not working in this situation. Please don't mark this as duplicate because I tried all of and not working so that's why I posted. Thanks in advance.

 <?php

error_reporting(E_ALL);
ini_set('display_errors', true);

header('Content-Type: text/html; charset=utf-8');

$pdo = new PDO('mysql:dbname=ourcms;host=localhost', 'root', '',
               array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

if (!empty($_POST['text'])) {
    $stmt = $pdo->prepare('INSERT INTO `texts` (`text`) VALUES (:text)');
    $stmt->execute(array('text' => $_POST['text']));
}

$results = $pdo->query('SELECT * FROM `texts`')->fetchAll(PDO::FETCH_ASSOC);

?>
<!DOCTYPE html>

<html>
<head>
    <title>UTF-8 encoding test</title>
</head>
<body>

<h1>Display test</h1>

<p>
A good day, World!<br>
Schönen Tag, Welt!<br>
Une bonne journée, tout le monde!<br>
يوم جيد، العالم<br>
좋은 일, 세계!<br>
Một ngày tốt lành, thế giới!<br>
こんにちは、世界!<br>
</p>

<h1>Submission test</h1>

<form action="" method="post" accept-charset="utf-8">
    <textarea name="text"></textarea>
    <input type="submit" value="Submit">
</form>

<?php if (!empty($_POST['text'])) : ?>
    <h2>Last received data</h2>
    <pre><?php echo htmlspecialchars($_POST['text'], ENT_NOQUOTES, 'UTF-8'); ?></pre>
<?php endif; ?>

<h1>Output test</h1>

<ul>
    <?php foreach ($results as $result) : ?>
        <li>
            <pre><?php echo htmlspecialchars($result['text'], ENT_NOQUOTES, 'UTF-8'); ?></pre>
        </li>
    <?php endforeach; ?>
</ul>

</body>
</html>
eightShirt
  • 1,457
  • 2
  • 15
  • 29
Computer Lover
  • 61
  • 1
  • 12
  • Your table is `tests` not `texts`? And you are trying to insert in to `texts` with the column of `text` instead of `test`? – Jon Apr 04 '16 at 18:54
  • o.O ! Thank you pros ! Where i was looking i don't know ! I followed this posts but there nothing i did seen ! [So the fact is here](http://stackoverflow.com/questions/35412343/php-error-call-to-a-member-function-fetchall-on-boolean) – Computer Lover Apr 04 '16 at 18:59

2 Answers2

1

$results = $pdo->query('SELECT * FROM texts')->fetchAll(PDO::FETCH_ASSOC);

Remove the chain and check the result of query() because for some reasons it returns false instead of PDOStatement:

Return Values 
PDO::query() returns a PDOStatement object, or FALSE on failure.

https://secure.php.net/manual/en/pdo.query.php

In general you should never assume query is successful, so always check for errors before continuing.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

mainly this situation got created max of times for typo error ! So before making any post about this problem. check your database typo errors and configuration.

Computer Lover
  • 61
  • 1
  • 12