gpt4 book ai didi

php - 奇怪的Mysql查询

转载 作者:太空宇宙 更新时间:2023-11-03 10:35:23 25 4
gpt4 key购买 nike

我从 PHP 运行这个查询:

"SELECT * FROM punti WHERE uud='2a1c866f-9f08-4c81-aeeb-48ec3b98ab43'"

$con = mysqli_connect( $serverd, $userd, $passd, $datad )or die( "Unable to connect to the MySQL Server!" );
//$uud = mysqli_real_escape_string( $con, urldecode( $_POST[ 'uud' ] ) );

$query = mysqli_query( $con, "SELECT * FROM punti WHERE uud='2a1c866f-9f08-4c81-aeeb-48ec3b98ab43'" );
$numrows = mysqli_num_rows( $query );
if ( $numrows > 0 ) {
$array = array();
while ( $row = mysqli_fetch_assoc( $query ) ) {
$data = $row[ 'data' ];
$punti = $row[ 'punti' ];
$array = array_merge($array, array($data => $punti));
}
echo json_encode( $array );
} else {
echo "Vuoto";
}

结果:{"35\/03\/2018":"3","31\/03\/2018":"6"}

它返回 2 个结果,但在数据库中有 4 个结果

看图理解:

enter image description here

同一查询的数据库结果:

enter image description here

最佳答案

问题出在您的 array_merge 函数中。合并数组时,您使用相同的string覆盖 值。你有相同的 string 键,它是 31/03/2018

使用简单的 [] 符号向结果添加新数组:

$array[] = array($data => $punti);
// instead
//$array = array_merge($array, array($data => $punti));

但请注意,您的 json 将具有另一种结构,例如:

[{"35\/03\/2018":"3"},{"31\/03\/2018":"6"},{"31\/03\/2018":"1"}]

关于php - 奇怪的Mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49589101/

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