gpt4 book ai didi

php - 从MySQL获取数据,不同查询的问题

转载 作者:行者123 更新时间:2023-11-29 17:56:03 25 4
gpt4 key购买 nike

我正在从 MySQL 数据库中以 JSON 格式获取一些数据,但遇到了一个奇怪的问题。

如果我写代码:

<?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 语句中包含“Tienda”或“Usuario”行,则不起作用。如果我只包含“id”行,效果很好......

最佳答案

我通过更改编码修复了它...

<?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的编码

关于php - 从MySQL获取数据,不同查询的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48827787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com