查询两个 Woocommerce 标准表

回答: 设法在下面回答我自己的问题,仍然欢迎反馈。

问题:我的说法有问题INNER JOIN。我的代码只用一张表就可以正常工作。

我的目标是根据用户输入表单的邮政编码查询zone_namefrom ,然后将其作为表的a 传递到我的查询函数。尽管我正在检查现有的 withing 表和 .zip中的匹配项,但我很难获得结果。woocommerce_shipping_zoneslocation_codewoocommerce_shipping_zone_locationswoocommerce_shipping_zone_locationszone_idwoocommerce_shipping_zones

任何帮助是极大的赞赏。

    <?php

        if(isset($_POST['checkZip']) && isset($_POST['submittedZip'])) {

            //is the cast to int needed?

            $submittedZip = (int)$_POST['submittedZip'];

            debug_to_console($submittedZip);

            prntPage($submittedZip);

        }


        //query for pulling in shipping data

        function prntPage($submittedZip) {

            global $wpdb;

            // The SQL query

            if($submittedZip) {

                $results =  $wpdb-> get_results("SELECT {$wpdb->prefix}woocommerce_shipping_zone_locations.location_code, {$wpdb->prefix}woocommerce_shipping_zones.zone_name ".

                            "FROM {$wpdb->prefix}woocommerce_shipping_zone_locations ".

                            "WHERE location_code = '$submittedZip' ".

                            "INNER JOIN {$wpdb->prefix}woocommerce_shipping_zones ".

                            "ON {$wpdb->prefix}woocommerce_shipping_zone_locations.zone_id = {$wpdb->prefix}woocommerce_shipping_zones.zone_id ");

                // Loop though rows data

                foreach( $results as $row ){

                    print_r($row);

                    debug_to_console($row);

                }

            }

        }


        //function to print to console, used for debugging php

        function debug_to_console($data) {

            $output = $data;

            $typeOfTest = getType($data);

            if (is_array($output))

                $output = implode(',', $output);

        

            echo "<script>console.log('Debug Objects: " . $output . ", Type: " . $typeOfTest . "' );</script>";

        }

    ?>


慕田峪4524236
浏览 95回答 1
1回答

四季花海

以下导致其工作。必须在声明之前进行连接WHERE。&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //query for pulling in shipping data&nbsp; &nbsp; &nbsp; &nbsp; function prntPage($submittedZip) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The SQL query&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($submittedZip) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results =&nbsp; $wpdb-> get_results("SELECT {$wpdb->prefix}woocommerce_shipping_zones.zone_name ".&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "FROM {$wpdb->prefix}woocommerce_shipping_zone_locations ".&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "INNER JOIN {$wpdb->prefix}woocommerce_shipping_zones ".&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ON {$wpdb->prefix}woocommerce_shipping_zone_locations.zone_id = {$wpdb->prefix}woocommerce_shipping_zones.zone_id ".&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "WHERE location_code = '$submittedZip' ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop though rows data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( $results as $row ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($row);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //debug_to_console($row);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP