0

I'm a beginner in PHP5 and I'would like to have some help to get the code correct. Check the code and I'will be grateful for your support. index.php:

if ($tag == 'AfficherMesAnnonce')
{
    $Id_Utilisateur= $_GET['Id_Utilisateur'];
    $annonce = $db->MesAnnonces($Id_Utilisateur);
    if ($annonce) 
    {
        $response["success"] = 1;
        $response["annonce"]["Departure_place"] =$annonce["Departure_place"];
        $response["annonce"]["Departure_date"] = $annonce["Departure_date"];
        $response["annonce"]["Arrival_place"] = $annonce["Arrival_place"];
        $response["annonce"]["Path_type"] = $annonce["Path_type"];
        echo json_encode($response);
        // annonce stored successfully

    }else
    {
    $response["error_msg"] ="No annonce found";
    return false;
    echo json_encode($response);
    }
}

DB_Function.php:

  public function MesAnnonces($Id_Utilisateur)
{
$result = mysql_query("SELECT * FROM annonce WHERE Utilisateur_Id_Utilisateur = $Id_Utilisateur") or die(mysql_error());
    // check for result 
        while($row = mysql_fetch_assoc($result))
        {
        $dd[]=$row;
       }
            $response=array('annonce'=> $dd);
            return $response;
}

and thank you in advance.

  • 1
    `mysql_*` functions are deprecated and your queries are vulnerable to sql injection. read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) –  Mar 20 '14 at 18:51
  • whats your problem actually ? – M.chaudhry Mar 20 '14 at 19:13

1 Answers1

0

You DB function is creating the dd array as a collection of rows, and then nesting that as an element of what's returned. But in your calling script you're assigning

$response["annonce"]["Departure_place"] = $annonce["Departure_place"]

--but the right side of that doesn't exist. You could instead assign:

$response["annonce"]["Departure_place"] = $annonce[0][0]["Departure_place"]
  • which would be the first row. You're not iterating through the rows in the client script, or using any kind of index to select a row.
BarryDevSF
  • 395
  • 3
  • 12