php - 从 2 个表中检索数据 PHP、MySql、Android

标签 php android mysql json

有谁知道我可以使用 PHP 从 MySql DB 中的 2 个表获取数据的方法吗?我的 Android 应用程序允许用户将商品放入购物车(MySql 中的表)。当用户单击应用程序中的购物车按钮时,它会加载“购物车 Activity ”,并根据“ session ID”或“客户 ID”加载 ListView ,其中包含他们添加到购物车的所有内容(如果用户已登录) .我现在要做的就是当商品添加到购物车时,我捕获产品 ID、产品名称、图像 URL、数量和价格。它效果很好而且很快。我想要做的是,由于产品名称、图像 url 和价格已经在“产品表”中,所以我想做同样的事情,但是当用户将商品添加到购物车时,仅捕获产品 ID 和 session ID 。当客户进入购物车 Activity 时,我希望我的 PHP 脚本获取 Shopping_Cart 表中的产品 ID,然后在产品表中搜索该产品 ID 并返回价格、图像 URL、名称等。

这是我的 PHP 脚本:

if($The_Function=="LOAD_CART"){
    $response = array();

    require_once __DIR__ . '/db_connect.php';

    $con = new DB_CONNECT();

    $user_session = $_GET['session'];
    $user_item_status = $_GET['status'];
    $order_total = 0.00;
    $result = mysql_query("SELECT * FROM shopping_cart WHERE SessionID LIKE '$user_session' AND Status LIKE '$user_item_status'");

    if(!empty($result)){
        if (mysql_num_rows($result) > 0) {
            $response["cart_contents"] = array();
            //$result = mysql_fetch_array($result);
            while ($row = mysql_fetch_array($result)) {
                $myItems = array();

                $myItems["product_id"] = $row["PID"];
                $myItems["product_name"] = $row["Item"];
                $myItems["product_price"] = $row["Price"];
                $myItems["product_qty"] = $row["Qty"];
                $myItems["image_url"] = $row["URL"];
                $myItems["line_id"] = $row["ItemCount"];
                $line_total = $row["Price"] * $row["Qty"];
                $order_total = $order_total + $line_total;
                array_push($response["cart_contents"], $myItems);
            }
            // success
            $response["success"] = 1;
            $response["order_total"] = $order_total;
            // user node
            //  $response["products"] = array();
            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }
}

这就是我正在尝试做的事情。此代码不起作用。

if($The_Function=="LOAD_CART2"){
    $response = array();

    require_once __DIR__ . '/db_connect.php';

    $con = new DB_CONNECT();

    $user_session = $_GET['session'];
    $user_item_status = $_GET['status'];
    $order_total = 0.00;
    $result = mysql_query("SELECT * FROM shopping_cart WHERE SessionID LIKE '$user_session' AND Status LIKE '$user_item_status'");

    if(!empty($result)){
        if (mysql_num_rows($result) > 0) {
            $response["cart_contents"] = array();
            //$result = mysql_fetch_array($result);
            while ($row = mysql_fetch_array($result)) {
                $line_total = $row["Price"] * $row["Qty"];
                $line_ID = $row["ItemCount"];
                $Quantity = $row["Qty"];
                $Prod_ID = $row["PID"];


                $result2 = mysql_query("SELECT * FROM products WHERE pid LIKE '$Prod_ID'");
                $myItems = array();
                $theItem=$row["name"];
                $thePrice=$row["price"];
                $theImage=$row["prod_image"];

                $myItems["line_id"] = $line_ID;
                $myItems["product_qty"] = $Quantity;
                $myItems["product_id"] = $Prod_ID;
                $myItems["product_name"] = $theItem;
                $myItems["product_price"] = $thePrice;
                $myItems["image_url"] = $theImage;

                $order_total = $order_total + $line_total;
                array_push($response["cart_contents"], $myItems);
            }




            }
            // success
            $response["success"] = 1;
            $response["order_total"] = $order_total;
            // user node
            //  $response["products"] = array();
            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }

这就是我刚刚尝试过的:

if($The_Function=="LOAD_CART2"){
    $response = array();

    require_once __DIR__ . '/db_connect.php';

    $con = new DB_CONNECT();

    $user_session = $_GET['session'];
    $user_item_status = $_GET['status'];
    $order_total = 0.00;

    $result = mysql_query("SELECT s.*, p.* FROM shopping_cart AS s LEFT JOIN products AS p ON p.pid = s.PID WHERE s.SessionID ='$user_session' AND s.Status = '$user_item_status'");

    if(!empty($result)){
        if (mysql_num_rows($result) > 0) {
            $response["cart_contents"] = array();
            //$result = mysql_fetch_array($result);
            while ($row = mysql_fetch_array($result)) {
                $myItems = array();
                //FROM shopping_cart Table
                $line_total = $row["Price"] * $row["Qty"];
                $myItems["line_id"] = $row["ItemCount"];
                $myItems["product_qty"] = $row["Qty"];
                $myItems["product_id"] = $row["PID"];

                //FROM product TABLE
                $myItems["product_name"]=$row["p.name"];
                $myItems["product_price"]=$row["p.price"];
                $myItems["image_url"]=$row["p.prod_image"];


            }

            $order_total = $order_total + $line_total;
            array_push($response["cart_contents"], $myItems);


            }
            // success
            $response["success"] = 1;
            $response["order_total"] = $order_total;
            // user node
            //  $response["products"] = array();
            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }

我的方案:

products TABLE

pid|name|price|created_at|prod_image|description|catagory



shopping_cart TABLE

FirstName|LastName|OrderNumber|CustomerID|Email|Price|Qty|Status|URL|PID|SessionID|CustomerType|ItemCount

最佳答案

您应该使用 LEFT JOIN,而不是对每个项目进行单独的查询。

类似于: “从 Shopping_cart AS 中选择 s.*, p.* LEFT JOIN 产品 AS p ON p.pid = s.PID WHERE s.SessionID ='$user_session' AND s.Status ='$user_item_status'

假设小写的pid在products表中,大写的pid在shopping_card表中。

关于php - 从 2 个表中检索数据 PHP、MySql、Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17498593/

相关文章:

java - 找不到 com.android.tools.build :gradle:4. 3.3

1 对 n 关系中的 MySQL 数据透视表

android - 如何在不同语言的给定文本文件中搜索字符串

android - 在 J2ME 和 Android 中,哪一种作为移动编程语言或框架更有用、更易用和更流行?

mysql - 清理 MySQL 表的表单数据

php - 在 CMS 中选择页面 View /布局

php - Imagemagick 无法读取文件 (MAMP)

php - MYSQL 在同一个表中选择

php - MySql 查询中基于初始查询结果的新查询

php - 一种不同的 Web 应用程序翻译方法