I m developping a mobile application with Ionic. when I try to select data from mysql, it works in my browser but when I use an external server it doesn't work (on my phone )!!! can someone help me :
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);
?>
I think that the problem in Json format because when I seperate the objects in different file.php it works , but I want to use one php file
Like @jay-blanchard said, you should use a library like PDO to run your queries, or a full-fledged ORM like Doctrine.
In order to show your data as Json, you could use json_encode to do that for you, instead of trying to render your Json manually.
So, assuming you are using Doctrine, you could do something like that:
$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
]);
problem solved :
I replace mysql_* to PDO :
<?php
require_once("Connexion.php");
header("Access-Control-Allow-Origin: *");
$request=json_decode(file_get_contents("php://input"));
$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 = $db->prepare($qry_em_1) or die('Could not query');
$result_2 = $db->prepare($qry_em_2) or die('Could not query');
$result_3 = $db->prepare($qry_em_3) or die('Could not query');
$result_4 = $db->prepare($qry_em_4) or die('Could not query');
$result_5 = $db->prepare($qry_em_5) or die('Could not query');
$result_6 = $db->prepare($qry_em_6) or die('Could not query');
$result_7 = $db->prepare($qry_em_7) or die('Could not query');
$result_1->execute();
$result_2->execute();
$result_3->execute();
$result_4->execute();
$result_5->execute();
$result_6->execute();
$result_7->execute();
$camion=$result_1->fetchAll(PDO::FETCH_NUM);
$bateau=$result_2->fetchAll(PDO::FETCH_NUM);
$transporteur=$result_3->fetchAll(PDO::FETCH_NUM);
$trajet=$result_4->fetchAll(PDO::FETCH_NUM);
$fournisseur=$result_5->fetchAll(PDO::FETCH_NUM);
$article=$result_6->fetchAll(PDO::FETCH_NUM);
$mTransport=$result_7->fetchAll(PDO::FETCH_NUM);
echo json_encode([
'camion' => $camion,
'bateau' => $bateau,
'transporteur' => $transporteur,
'trajet' => $trajet,
'fournisseur' => $fournisseur,
'article' => $article,
'moyenTransport' => $mTransport
]);
$db = null;
?>
connection.php
<?php
$hostname = "";
$username = "";
$database="";
$password = "";
try {
$db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
thank you guys for your advices