【问题标题】:Can't read response from PHP API无法读取来自 PHP API 的响应
【发布时间】:2020-12-10 16:52:37
【问题描述】:

我用 PHP 编写了一个 API,但我无法得到响应,之前我在另一个共享主机中使用了相同的 API,但是当我将服务器更改为另一个 go-daddy 共享主机时,一些 API 正在工作,但其他 API 没有.我不知道为什么会这样。使用邮递员,当我响应数组长度时,它得到正确的数组长度为 14(我的列数),但没有显示结果。请帮我解决问题

 public function viewstock($token, $shopid)
{
    $res = array();
    $userId = $this->tokenVerification($token);
    $delstatus = 0;
    $ProductList = array();
    $data = array();

    if ($userId > 0) {
        if ($stmt = $this->con->prepare("SELECT s.product_id,s.shop_id,s.product_category_id,s.display_order,s.sku,s.product_name,s.description,s.pro_type,s.is_timing,s.dateinfo,c.category_name,s.is_spe_offer,s.is_active FROM shop_stock s, category c WHERE s.product_category_id=c.id AND s.shop_id=? AND s.is_delete=?")) {

            $stmt->bind_param("ii", $shopid, $delstatus);
            $stmt->execute();
            $stmt->bind_result($product_id, $sho_id, $category_id, $display_order, $sku, $product_name, $product_desc, $pro_type, $pro_availability, $dateinfo, $category_name, $is_offer, $is_active);

            while ($stmt->fetch()) {
                $temp = array();
                $temp['product_id'] = $product_id;
                $temp['shopid'] = $sho_id;
                $temp['category_id'] = $category_id;
                $temp['displayorder'] = $display_order;
                $temp['sku'] = $sku;
                $temp['product_name'] = $product_name;
                $temp['product_desc'] = $product_desc;
                $temp['pro_type'] = $pro_type;
                $temp['pro_availability'] = $pro_availability;
                $temp['dateinfo'] = $dateinfo;
                $temp['product_category'] = $category_name;
                $temp['is_offer'] = $is_offer;
                $temp['pro_status'] = $is_active;
                array_push($data, $temp);
            }
            /* free results */
            $stmt->free_result();

            /* close statement */
            $stmt->close();

            foreach ($data as &$value) {
                $StockList = array();
                $StockList = $value;
                $stmt2 = $this->con->prepare("SELECT id,image,dateinfo FROM product_images WHERE product_id=? order by id");
                $stmt2->bind_param("i", $value['product_id']);
                $stmt2->execute();
                $stmt2->bind_result($id, $image, $dateinfo);
                $imagelist = array();
                while ($stmt2->fetch()) {
                    $temp1 = array();
                    $temp1['image_id'] = $id;
                    $temp1['image'] = $image;
                    $temp1['dateinfo'] = $dateinfo;
                    array_push($imagelist, $temp1);
                }
                $StockList["imagelist"] = $imagelist;

                $stmt3 = $this->con->prepare("SELECT p.id, p.variant_name as pro_variant,p.mrp,p.selling_price,p.offerpercent,p.stock_quantity,v.id,v.variant_name,p.stock_limit,p.unit FROM product_variant p, variants v WHERE p.variant_id=v.id AND p.product_id=?");
                $stmt3->bind_param("i", $value['product_id']);
                $stmt3->execute();
                $stmt3->bind_result($id, $pro_variant, $mrp, $sellingprice, $offerpercent, $stock_quantity, $variant_id, $variant_name, $stock_limit, $unit);
                $data2 = array();
                while ($stmt3->fetch()) {
                    $temp5 = array();
                    $temp5['id'] = $id;
                    $temp5['pro_variant'] = $pro_variant;
                    $temp5['mrp'] = $mrp;
                    $temp5['selling_price'] = $sellingprice;
                    $temp5['offerpercent'] = $offerpercent;
                    $temp5['stock_quantity'] = $stock_quantity;
                    $temp5['variant_id'] = $variant_id;
                    $temp5['variant_name'] = $variant_name;
                    $temp5['stock_limit'] = $stock_limit;
                    $temp5['unit'] = $unit;
                    array_push($data2, $temp5);

                    if (!$temp5['stock_quantity'] == 0) {

                        $flag = 1;
                    }
                }
                $StockList["variantlist"] = $data2;
                if ($flag == 1) {
                   
                    array_push($ProductList, $StockList);

                }
                

            }
            /* free results */
            $stmt2->free_result();

            /* close statement */
            $stmt2->close();
            /* free results */
            $stmt3->free_result();

            /* close statement */
            $stmt3->close();
            if (count($ProductList) <= 0) {
                 
                $res['st'] = 'no';
                $res['data'] = 'No Data';
            } else {
                
                $res['st'] = 'yes';
                $res['data'] = $ProductList;
            }
            return $res;

        } else {
            // $error = $mysqli->errno . ' ' . $mysqli->error;
            // echo $error;
        }

    } else if ($userId == 0) {
        //token verification expired
        //echo "Expired";
        $res['st'] = 'expire';
        $res['data'] = 'No Data';
        return $res;
    }

}

这是一个不工作的 API,它使用 MYSQL 作为我的数据库。

当我使用 pirnt_r() 时,我可以得到回复,但为什么它没有显示在我的回复中

public function categorylist($token)
{
    $res = array();
    $userId = $this->tokenVerification($token);
    $delstatus = 0;
    $noparent = "None";
    if ($userId > 0) {
        $stmt = $this->con->prepare("SELECT p.id,p.category_name,p.display_order,p.dateinfo,IFNull((SELECT category_name from category WHERE p.parent_id=id),?)as parent,p.image FROM category p WHERE p.is_delete=? ORDER BY p.id DESC ");
        $stmt->bind_param("si", $noparent, $delstatus);
        $stmt->execute();
        $stmt->bind_result($id, $category_name, $displayorder, $dateinfo, $parent, $image);
        $CategoryList = array();

        while ($stmt->fetch()) {
            $temp = array();
            $temp['category_id'] = $id;
            $temp['main_name'] = $parent;
            $temp['category_name'] = $category_name;
            $temp['display_order'] = $displayorder;
            $temp['dateinfo'] = $dateinfo;
            $temp['image'] = $image;
            array_push($CategoryList, $temp);
        }
        if (count($CategoryList) <= 0) {
            $res['st'] = 'no';
            $res['data'] = 'No Data';
        } else {
            $res['st'] = 'yes';
            $res['data'] = $CategoryList;
        }
        /* free results */
        $stmt->free_result();

        /* close statement */
        $stmt->close();
        return $res;
    } else if ($userId == 0) {
        //token verification expired
        //echo "Expired";
        $res['st'] = 'expire';
        $res['data'] = 'No Data';
        return $res;
    }

}

此代码正在运行并从同一服务器获取结果

【问题讨论】:

  • 你需要echo一些回应!这也是一个函数,所以你怎么调用它,显然你不能直接从 AJAX 调用中调用它
  • 如果您查看 JOINing 表,您可以在一个查询中收集所有数据
  • 我有另一个索引文件来调用这个函数,如果我给 echo "somevalue" 我可以打印;但不是回复

标签: php mysql arrays api


【解决方案1】:

您需要使用json_encode();$res; 数组进行编码,然后您应该回显该json 编码字符串,之后您可以将其作为API 访问,每个请求都可以访问您的函数并返回结果

谢谢,

【讨论】:

猜你喜欢
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 2012-12-07
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多