找到你要的答案

Q:PHP SLIM: api call returns 6x same array

Q:PHP苗条:API调用返回相同的阵列6x

I am using php slim to generate API calls to receive data from my MySQL database.

//PHPSLIM
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();


$categories = new \Slim\Slim();
$categories = \Slim\Slim::getInstance();

$categories->get('/categories', function(){
    global $conn, $servername, $username, $password, $dbname;
    mysql_connect($servername, $username, $password);
    mysql_select_db($dbname);
    $result = mysql_query("SELECT `categoryName` FROM `category`");

    while($line = mysql_fetch_array($result, MYSQL_BOTH)) {
        $cat[] = $line[0];
    } 

    echoResponseCat(200, $cat);

});

function echoResponseCat($status_code, $response) {
    global $categories;
    $categories->status($status_code);
    header('Content-Type: application/json');
    $categories->contentType('application/json');
    echo json_encode($response,JSON_NUMERIC_CHECK);
}

$categories->run();

$app = new \Slim\Slim();
$app = \Slim\Slim::getInstance();

//GET ALL ORDERS
$app->get('/orders', function(){
...
}

When I surf to http://localhost/localsites/serf/wea/web/api/categories I get the data in json but it gives me 6 times the array!

["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]

How is this coming and how can it be solved?

我使用PHP苗条生成API调用我的MySQL数据库接收数据。

//PHPSLIM
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();


$categories = new \Slim\Slim();
$categories = \Slim\Slim::getInstance();

$categories->get('/categories', function(){
    global $conn, $servername, $username, $password, $dbname;
    mysql_connect($servername, $username, $password);
    mysql_select_db($dbname);
    $result = mysql_query("SELECT `categoryName` FROM `category`");

    while($line = mysql_fetch_array($result, MYSQL_BOTH)) {
        $cat[] = $line[0];
    } 

    echoResponseCat(200, $cat);

});

function echoResponseCat($status_code, $response) {
    global $categories;
    $categories->status($status_code);
    header('Content-Type: application/json');
    $categories->contentType('application/json');
    echo json_encode($response,JSON_NUMERIC_CHECK);
}

$categories->run();

$app = new \Slim\Slim();
$app = \Slim\Slim::getInstance();

//GET ALL ORDERS
$app->get('/orders', function(){
...
}

When I surf to http://localhost/localsites/serf/wea/web/api/categories I get the data in json but it gives me 6 times the array!

["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]["Lattes","CC Blend","Ice tea","Frappes"]

这是怎么来的,如何解决?

answer1: 回答1:

change this line

// while($line = mysql_fetch_array($result, MYSQL_BOTH)) {
while($line = mysql_fetch_array($result, MYSQL_NUM)) {

改变这一行

// while($line = mysql_fetch_array($result, MYSQL_BOTH)) {
while($line = mysql_fetch_array($result, MYSQL_NUM)) {
php  mysql  slim