0

i have online store and i want to make an api for it i have a three tables in MySql database i Should use to fetch the product details file_path for the product image url invetory_details for price tbl_product for the product details like (name , description, digital or physical...etc) i didn't know how to fetch data from more than one table please help me with this. this is my code i was able to fetch data from one table

  define('DB_HOST','localhost');
  define('DB_USER','admin');
  define('DB_PASS','123456');
  define('DB_NAME','testname');
  
  $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  if (mysqli_connect_errno()){
      die('error'.mysqli_connect_error());
  }
  $stmt = $conn->prepare( "SELECT id, upc_code, category_id, sub_category_id, sub_subcategory_id, name, description, brand_id , digital, license_key_code, active , added_by_id, sort_order, rating, added_at, updated_at FROM tbl_product;");
  $stmt->execute();
  $stmt->bind_result($id, $upc_code, $category_id, $sub_category_id, $sub_subcategory_id, $name, $description, $brand_id, $digital, $license_key_code, $active, $added_by_id, $sort_order, $rating, $added_at, $updated_at);
  $product= array();
  
  while($stmt->fetch()){
      $temp=array();
      $temp['id']=$id;
      $temp['upc_code']=$upc_code;
      $temp['category_id']=$category_id;
      $temp['sub_category_id']=$sub_category_id;
      $temp['sub_subcategory_id']=$sub_subcategory_id;
      $temp['name']=$name;
      $temp['digital']=$digital;
      $temp['active']=$active;
      $temp['added_by_id']=$added_by_id;
      $temp['sort_order']=$sort_order;
      $temp['rating']=$rating;
      $temp['added_at']=$added_at;
      $temp['updated_at']=$updated_at;
      
      array_push($product, $temp);
  }
  echo json_encode($product);
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Fetchng results from multiple tables is done the same way as fetching from one table. Processing the results may be different. Pls describe what you would like to do with the fetched data and if these multiple tables relate to each other. – Shadow Jul 22 '20 at 00:33
  • hello and thank you for your time yes the tables are connected to each other , i want to fetch data from a multiple tables to get the the product details and then use it to build an android app please check this link [link](https://digtalstore.com/Api/api.php) – Yazan Alaws Jul 22 '20 at 00:43
  • Then you need to use a join to link these tables together on common field(s). – Shadow Jul 22 '20 at 00:49
  • can you please writ me an example? i am a beginner in this domain thank you for your assistance i really appreciate it – Yazan Alaws Jul 22 '20 at 01:18
  • Pls check out the answers to the duplicate question for an example. – Shadow Jul 22 '20 at 05:18

0 Answers0