定义连接数据库的常量
define("DB_SERVER", "127.0.0.1");
define("DB_USER", "chutnej_app");
define("DB_PASS", "1234");
define("DB_NAME", "chutnej");
创建数据库连接
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
这是mysql中的表
CREATE TABLE category (
category_id INT NOT NULL AUTO_INCREMENT,
category_name VARCHAR(255) UNIQUE,
PRIMARY KEY (category_id)
)
ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;
用一些数据填充表:
INSERT INTO category (category_name) VALUES ('Reportáže'), ('Rozhovory'), ('Místa');
我调用此函数从表中选择上述数据
function get_category() {
global $connection;
$query = "SELECT * FROM category";
$categories = mysqli_query($connection, $query);
confirm_query($categories);
while($category = mysqli_fetch_array($categories)) {
echo "<option value=\"{$category['category_id']}\">{$category['category_name']} </option>";
}
}
结果:
<li>
<label for="article_category">Druh článku:</label><br />
<select name="article_category" id="article_category" type="text">
<option value="3">M�sta</option><option value="1">Report��e</option><option value="2">Rozhovory</option>
</select>
</li>
这就是我在浏览器中得到的,所有浏览器都这样做。浏览器中的编码设置为 UTF-8。谁能解释我是什么导致了这个问题?至少让我朝着正确的方向前进,这就足够了:) 我认为只要在CREATE TABLE
语句中指定 UTF8,我就可以解决编码问题。