使用json从mysql中选择数据

时间:2015-08-14 12:17:05

标签: php mysql json ionic-framework ionic

我正在使用Ionic开发移动应用程序。当我尝试从mysql中选择数据时,它可以在我的浏览器中运行,但是当我使用外部服务器时,它不起作用(在我的手机上)! 有人可以帮助我:

ExtractData.php:

<?php

header("Access-Control-Allow-Origin: *");

$request=json_decode(file_get_contents("php://input"));
$con=mysql_connect("","","") ;//or die(mysql_error());
mysql_select_db("myDbB",$con);

$qry_em_1 = "SELECT id,matricule,idTransporteur FROM camion";
$qry_em_2 = "SELECT id,matricule FROM bateau";
$qry_em_3 = "SELECT id,codeTransporteur FROM transporteur";
$qry_em_4 = "SELECT id,designation FROM trajet";

$qry_em_5 = "SELECT id,codeFr FROM fournisseur";
$qry_em_6 = "SELECT codeArticle,description FROM article";
$qry_em_7 = "SELECT id,description FROM moyenTransport";

$result_1 = mysql_query($qry_em_1) or die('Could not query');
$result_2 = mysql_query($qry_em_2) or die('Could not query');
$result_3 = mysql_query($qry_em_3) or die('Could not query');
$result_4 = mysql_query($qry_em_4) or die('Could not query');
$result_5 = mysql_query($qry_em_5) or die('Could not query');
$result_6 = mysql_query($qry_em_6) or die('Could not query');
$result_7 = mysql_query($qry_em_7) or die('Could not query');

  //encode camion data
if(mysql_num_rows($result_1)){
    echo '{"camion":[';

    $first = true;
    while($row=mysql_fetch_row($result_1)){

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        echo json_encode($row);
    }
    echo '],';
} else {
    echo '[]';
}

//encode bateau data
if(mysql_num_rows($result_2)){
    echo '"bateau":[';

    $first = true;
    while($row=mysql_fetch_row($result_2)){

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        echo json_encode($row);
    }
    echo '],';
} else {
    echo '[]';
}

//encode transporteur data
if(mysql_num_rows($result_3)){
    echo '"transporteur":[';

    $first = true;
    while($row=mysql_fetch_row($result_3)){

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        echo json_encode($row);
    }
echo '],';
} else {
    echo '[]';
}

//encode trajet data

if(mysql_num_rows($result_4)){
    echo '"trajet":[';

    $first = true;
    while($row=mysql_fetch_row($result_4)){

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        echo json_encode($row);
    }
    echo '],';
} else {
    echo '[]';
}

//encode fournisseur data
if(mysql_num_rows($result_5)){
    echo '"fournisseur":[';

    $first = true;
    while($row=mysql_fetch_row($result_5)){

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        echo json_encode($row);
    }
echo '],';
} else {
    echo '[]';
}

//encode article data
if(mysql_num_rows($result_6)){
    echo '"article":[';

    $first = true;
    while($row=mysql_fetch_row($result_6)){

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        echo json_encode($row);
    }
echo '],';
} else {
    echo '[]';
}

//encode transport data

if(mysql_num_rows($result_7)){
    echo '"moyenTransport":[';

    $first = true;
    while($row=mysql_fetch_row($result_7)){

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        echo json_encode($row);
    }
    echo ']}';
} else {
    echo '[]';
}

mysql_close($con);

?>

我认为Json格式的问题是因为当我在不同的file.php中分隔对象时它可以工作,但我想使用一个php文件

1 个答案:

答案 0 :(得分:0)

就像@ jay-blanchard所说的那样,你应该使用像PDO这样的库来运行你的查询,或像Doctrine那样使用完整的ORM。

为了将您的数据显示为Json,您可以使用json_encode为您执行此操作,而不是尝试手动渲染Json。

因此,假设您使用的是Doctrine,您可以这样做:

$camion       = $entityManager->createQuery($qry_em_1)->getArrayResult();
$bateau       = $entityManager->createQuery($qry_em_2)->getArrayResult();
$transporteur = $entityManager->createQuery($qry_em_3)->getArrayResult();
$trajet       = $entityManager->createQuery($qry_em_4)->getArrayResult();
$fournisseur  = $entityManager->createQuery($qry_em_5)->getArrayResult();
$article      = $entityManager->createQuery($qry_em_6)->getArrayResult();
$mTransport   = $entityManager->createQuery($qry_em_7)->getArrayResult();

return json_encode([
    'camion'         => $camion,
    'bateau'         => $bateau,
    'transporteur'   => $transporteur,
    'trajet'         => $trajet,
    'fournisseur'    => $fournisseur,
    'article'        => $article,
    'moyenTransport' => $mTransport   
]);