我以JSON格式从MySQL数据库中获取一些数据,但我有一个奇怪的问题。
如果我写代码:
<?php
//Step1 - Access Database
ini_set( "display_errors", "1" );
$db = mysqli_connect('-','-','-','-')
or exit(mysqli_connect_error());
//Step2 - Create query and execute it
$query = "SELECT `id` FROM `Tiendas`";
$result = mysqli_query($db, $query) or exit(mysqli_error($db));
//Step 3 - Configure results
$data = array();
while ( $row = $result->fetch_assoc() ) {
$data[] = $row;
}
//Step 4 - Close connection and send results
header( 'Content-Type: application/json' );
mysqli_close($db);
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>
效果很好,但是如果我写的话:
<?php
//Step1 - Access Database
ini_set( "display_errors", "1" );
$db = mysqli_connect('-','-','-','-')
or exit(mysqli_connect_error());
//Step2 - Create query and execute it
$query = "SELECT `Tienda` FROM `Tiendas`";
$result = mysqli_query($db, $query) or exit(mysqli_error($db));
//Step 3 - Configure results
$data = array();
while ( $row = $result->fetch_assoc() ) {
$data[] = $row;
}
//Step 4 - Close connection and send results
header( 'Content-Type: application/json' );
mysqli_close($db);
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>
它没有显示任何内容。都没有错误代码......
表格如下:
CREATE TABLE `Tiendas` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`Usuario` VARCHAR(50) NULL DEFAULT NULL,
`Tienda` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=217
;
如果我在SELECT语句中包含&#34; Tienda&#34;它不起作用。或者&#34; Usuario&#34;行。如果我只包含&#34; id&#34;排它很好......
答案 0 :(得分:0)
我修改了它改变了编码......
<?php
//Step1 - Access Database
ini_set( "display_errors", "1" );
$db = mysqli_connect('localhost','fgallegof','qU5v6f5$','cdm')
or exit(mysqli_connect_error());
mysqli_set_charset($db, "utf8");
//Step2 - Create query and execute it
$query = "SELECT `Tienda` FROM `Tiendas`";
$result = mysqli_query($db, $query) or exit(mysqli_error($db));
//Step 3 - Configure results
$data = array();
while ( $row = $result->fetch_assoc() ) {
$data[] = $row;
}
//var_dump($data);
//Step 4 - Close connection and send results
header( 'Content-Type: application/json' );
mysqli_close($db);
echo json_encode($data);
?>
我需要添加mysqli_set_charset($db, "utf8");
并修改相对于JSON