Fetch only one value.i want to fetch all value from DB

i want to fetch all value based on user id.

.ts

getValue() {

  /*****************************************************/
        this.storage.get('user_id').then((value) => {
        this.userid=value;
  
  
     this.http.get("http://www.mydmain.com/addsApp/append/index.php/Myads/"+this.userid)
     
      .subscribe((res:Response)=>{
      let dataTemp = res.json(); //dataTemp is the json object you showed me
      this.myads = dataTemp.MyAd; //contains myads [] object with all yours user
      //alert(this.myads.ad_store_id);
      this.storage.set('adsPostId', this.myads.ad_store_id);
})//subscribe
});
 }

html

<ion-content padding>
<ion-card *ngIf="myads">
<ion-card-header>      
<span item-right> 
  <div class="pull-right">
    <ion-icon (click)="EditAds()" name="create"></ion-icon>
  </div>
</span>
</ion-card-header>
  <ion-item>
    <ion-avatar item-start>
    <img src="http://www.mydomain.com/addsApp/append/image-folders/{{myads.img}}">
    </ion-avatar>
     Product Name<h1>{{myads.addtitle}}</h1>
     <br>
     Description:<h4>{{myads.description}}</h4>
     <br>
     <h4>Price:<strong>{{myads.price}}</strong></h4>

  </ion-item>
  <ion-item>
  </ion-item>
</ion-card>
</ion-content>

This my table

Below my php code .



  function MyAd($userid)
  {
   
   $sql_query = "select * from ad_store where user_id='$userid'";
   try
   {
    $con = new Connection;
    $dbCon = $con->getConnection();
    $stmt  = $dbCon->query($sql_query);
    $MyAds = $stmt->fetch(PDO::FETCH_OBJ); 
    $dbCon = null;
  echo '{"MyAd": ' . json_encode($MyAds) . '}';
   }
   catch(PDOException $e) 
   {
    echo '{"error":{"text":'. $e->getMessage() .'}}';
   }
  }

what i’m wrong.i’m getting only one json response but there is two response .

Hi, @flycoders_sourav
If you want fetch one json, so you want add in Query LIMIT 1

  function MyAd($userid)
  {
   
   $sql_query = "select * from ad_store where user_id='$userid' LIMIT 1";
   try
   {
    $con = new Connection;
    $dbCon = $con->getConnection();
    $stmt  = $dbCon->query($sql_query);
    $MyAds = $stmt->fetch(PDO::FETCH_OBJ); 
    $dbCon = null;
  echo '{"MyAd": ' . json_encode($MyAds) . '}';
   }
   catch(PDOException $e) 
   {
    echo '{"error":{"text":'. $e->getMessage() .'}}';
   }
  }

OR
if you want fetch All json, so you changed

  function MyAd($userid)
  {
   
   $sql_query = "select * from ad_store where user_id='$userid' LIMIT 1";
   try
   {
    $con = new Connection;
    $dbCon = $con->getConnection();
    $stmt  = $dbCon->query($sql_query);
    $MyAds = $stmt->fetchAll(); 
    $dbCon = null;
  echo '{"MyAd": ' . json_encode($MyAds) . '}';
   }
   catch(PDOException $e) 
   {
    echo '{"error":{"text":'. $e->getMessage() .'}}';
   }
  }

Hope, this will resolve your issue
Thanks ,

No sir.actually i want to fetch all value based on userid
thats why. am using this below query.
$sql_query = "select * from ad_store where user_id='$userid'
Here two row.but showing only one value into my console.

i want to fetch n number of row.

this is my console out put.

but sir there is two row into my table. but i don’t know why return only one row.

I guess, you first have to check exactly what’s your query is returning

select * from ad_store where user_id=2

You might have to -

1 use FetchAll PDO function
2) Use For/While loop to append all rows in one array
3) Then use Json decode to return to your Rest Call

Hope it helps

when run the query in sql command .its working fine.
please give me a sample of code actually i’m new in PDO.

Check this http://php.net/manual/en/pdostatement.fetchall.php

It will give you all the details you required.

Also not to nitpick but this really isn’t an Ionic question.

You would get I imagine a much better level of support from a PHP board or stackoverflow.

1 Like

Thank you so much @viveknarula . its working fine.
Below is my previous code
$MyAds = $stmt->fetch(PDO::FETCH_OBJ);
Now changed fetch(PDO::FETCH_OBJ); to fetchAll(PDO::FETCH_OBJ);
$MyAds = $stmt->fetchAll(PDO::FETCH_OBJ);