mysql上的Php脚本查询


Php script query on mysql

所以我使用php将我的android应用程序连接到mysql数据库。我在几个小型数据库上测试了应用程序和查询。一切都很好。在较大的数据库上使用旧查询会使php查询永远加载在web浏览器中,从而不会显示预期的JSON。所以我做了一个新的查询,但它不会在应用程序中显示任何结果,而且它只显示查询的最后一个结果。有什么方法可以正确地解决这个问题吗?

旧的php查询在小数据库上工作,但在具有大数据库的web浏览器中永远加载:

<?php
error_reporting(0);
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("SELECT eid,marca,investimento,marcatotal,dataconstituicao,datainicio FROM empresa") or die(mysql_error());
header('content-type: application/json; charset=utf-8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
if (mysql_num_rows($result) > 0) {
    $response["empresa"] = array();
    while ($row = mysql_fetch_array($result)) {
        $product = array();
        $product["eid"] = $row["eid"];
        $product["marca"] = $row["marca"];
        $product["investimento"] = $row["investimento"];
        $product["dataconstituicao"] = $row["dataconstituicao"];
        $product["datainicio"] = $row["datainicio"];
        array_push($response["empresa"], $product);
    }
    $response["success"] = 1;
    echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "No empresa found";
    echo json_encode($response);
}
?>

我创建了一个新的php查询,它只在web浏览器中显示最后一个结果,在新的更大的数据库上不显示任何内容:

<?php
//error_reporting(E_ALL);
error_reporting(0);
require_once('db_config.php');
$conn = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
$db=  mysql_select_db($db_database, $conn);
$sql="SELECT eid,marca,investimento,marcatotal,dataconstituicao,datainicio FROM empresa" ;
header('content-type: application/json; charset=utf-8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
$rs = mysql_query($sql,$conn);
while ($row=mysql_fetch_assoc($rs))
{
    $i=0;
    foreach($row as $key => $value)   
    {
        if (is_string($key))
        {
         $fields[mysql_field_name($rs,$i++)] = $value;
        }  
    }
    $json_result ["empresa"] =  $fields;
}
$response = json_encode($json_result);
print_r($response);
?>

Patrick Q的回答是正确的:$json_result["empresa"]必须是$json_recurt["empresa"][]=$fields;此外,他的建议帮助我进一步解决了我的问题,因为我刚刚从我的应用程序中删除了成功=1逻辑,以便它发挥作用。谢谢大家的帮助!