应用程序在MySQL数据库中测试编码而不是提交到表

时间:2012-08-14 15:42:30

标签: mysql utf-8 character-encoding

我从this page获得了以下应用程序,以便我可以在MySQL数据库上测试字符编码。我使用同一页面上列出的查询创建了数据库,即:

CREATE TABLE `texts` (
  `id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
  `content` TEXT,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

并输入了连接到我的数据库的详细信息,我确信这是正确的。但是,由于某种原因没有任何东西被提交到我的数据库,当我手动向数据库添加一个条目时,我在“输出测试”下面收到以下错误:

  

注意:未定义的索引:/home/..../php/encoding_test.php中的文本   第56行

我想知道是否有人可以发现问题可能是什么?

<?php

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

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

$pdo = new PDO('mysql:dbname=encoding_test;host=localhost', 'user', 'pass',
               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>

1 个答案:

答案 0 :(得分:1)

ALTER TABLE `texts` CHANGE `content` `text` TEXT;

根据您的PHP代码,您使用表texts,因为它包含名为text的列,但在您的架构中,此列的名称为content 因此,您需要使用上述查询将列content重命名为text