php中mysql的双输出

时间:2015-05-05 13:46:57

标签: php mysql arrays json string

我从opencart安装中选择表名。 尝试将结果(单列)输出为格式化为JSON数组的字符串。 这是代码:

<?php
function getTableNames()
    {
    //Change these variables depending on the server it will be used on
    $server = 'localhost';
    $user = 'User1';
    $pass = 'pass';
    $DBName = 'OpenCartTest';

    //returns a single column of all tables in a mysql database named "OpenCartTest"
    $tablesGetSQL = 'Select `table_name` from  `information_schema`.`TABLES` where `table_schema` like "OpenCartTest" AND `table_name LIKE "oc_customer%"';
    $conn = new mysqli($server, $user, $pass, $DBName);
    $JSONStringArray = '[';
    if(mysqli_connect_errno())
    {
        echo '---unauthorized---';
    }
    else
    {
        $result = $conn->query($tablesGetSQL);
        $l = $result->num_rows;
        if($l > 0)
        {
            //for($i = 0;$i < $l;$i++)
            while($row = $result->fetch_assoc())
            {
                //$row = $result->fetch_assoc();
                foreach($row as $item)
                {
                    //echo $item . "|";
                    $JSONStringArray .= "'" . $item . "',";
                }
            }
        }
        $JSONStringArray .= substr($JSONStringArray, 0, -1) . ']';
        $conn->close();
        return $JSONStringArray;
    }
}
$output = getTableNames();
echo substr_count($output, "[") . '<br>';
echo $output;
?>

由于某种原因,输出加倍,有2&#39; [&#39;,但我只将它设置为字符串一次。也没有关闭&#39;]&#39;在第一行。

输出:

2
['oc_address','oc_affiliate','oc_affiliate_activity','oc_affiliate_login','oc_affiliate_transaction','oc_api','oc_attribute','oc_attribute_description','oc_attribute_group','oc_attribute_group_description','oc_banner','oc_banner_image','oc_banner_image_description','oc_category','oc_category_description','oc_category_filter','oc_category_path','oc_category_to_layout','oc_category_to_store','oc_country','oc_coupon','oc_coupon_category','oc_coupon_history','oc_coupon_product','oc_currency','oc_custom_field','oc_custom_field_customer_group','oc_custom_field_description','oc_custom_field_value','oc_custom_field_value_description','oc_customer','oc_customer_activity','oc_customer_ban_ip','oc_customer_group','oc_customer_group_description','oc_customer_history','oc_customer_ip','oc_customer_login','oc_customer_online','oc_customer_reward','oc_customer_transaction','oc_download','oc_download_description','oc_event','oc_extension','oc_filter','oc_filter_description','oc_filter_group','oc_filter_group_description','oc_geo_zone','oc_information','oc_information_description','oc_information_to_layout','oc_information_to_store','oc_language','oc_layout','oc_layout_module','oc_layout_route','oc_length_class','oc_length_class_description','oc_location','oc_manufacturer','oc_manufacturer_to_store','oc_marketing','oc_modification','oc_module','oc_option','oc_option_description','oc_option_value','oc_option_value_description','oc_order','oc_order_custom_field','oc_order_fraud','oc_order_history','oc_order_option','oc_order_product','oc_order_recurring','oc_order_recurring_transaction','oc_order_status','oc_order_total','oc_order_voucher','oc_osapi_last_modified','oc_product','oc_product_attribute','oc_product_description','oc_product_discount','oc_product_filter','oc_product_image','oc_product_option','oc_product_option_value','oc_product_recurring','oc_product_related','oc_product_reward','oc_product_special','oc_product_to_category','oc_product_to_download','oc_product_to_layout','oc_product_to_store','oc_recurring','oc_recurring_description','oc_return','oc_return_action','oc_return_history','oc_return_reason','oc_return_status','oc_review','oc_setting','oc_stock_status','oc_store','oc_tax_class','oc_tax_rate','oc_tax_rate_to_customer_group','oc_tax_rule','oc_tg_tglite_revolution_slider','oc_upload','oc_url_alias','oc_user','oc_user_group','oc_voucher','oc_voucher_history','oc_voucher_theme','oc_voucher_theme_description','oc_weight_class','oc_weight_class_description','oc_zone','oc_zone_to_geo_zone',
['oc_address','oc_affiliate','oc_affiliate_activity','oc_affiliate_login','oc_affiliate_transaction','oc_api','oc_attribute','oc_attribute_description','oc_attribute_group','oc_attribute_group_description','oc_banner','oc_banner_image','oc_banner_image_description','oc_category','oc_category_description','oc_category_filter','oc_category_path','oc_category_to_layout','oc_category_to_store','oc_country','oc_coupon','oc_coupon_category','oc_coupon_history','oc_coupon_product','oc_currency','oc_custom_field','oc_custom_field_customer_group','oc_custom_field_description','oc_custom_field_value','oc_custom_field_value_description','oc_customer','oc_customer_activity','oc_customer_ban_ip','oc_customer_group','oc_customer_group_description','oc_customer_history','oc_customer_ip','oc_customer_login','oc_customer_online','oc_customer_reward','oc_customer_transaction','oc_download','oc_download_description','oc_event','oc_extension','oc_filter','oc_filter_description','oc_filter_group','oc_filter_group_description','oc_geo_zone','oc_information','oc_information_description','oc_information_to_layout','oc_information_to_store','oc_language','oc_layout','oc_layout_module','oc_layout_route','oc_length_class','oc_length_class_description','oc_location','oc_manufacturer','oc_manufacturer_to_store','oc_marketing','oc_modification','oc_module','oc_option','oc_option_description','oc_option_value','oc_option_value_description','oc_order','oc_order_custom_field','oc_order_fraud','oc_order_history','oc_order_option','oc_order_product','oc_order_recurring','oc_order_recurring_transaction','oc_order_status','oc_order_total','oc_order_voucher','oc_osapi_last_modified','oc_product','oc_product_attribute','oc_product_description','oc_product_discount','oc_product_filter','oc_product_image','oc_product_option','oc_product_option_value','oc_product_recurring','oc_product_related','oc_product_reward','oc_product_special','oc_product_to_category','oc_product_to_download','oc_product_to_layout','oc_product_to_store','oc_recurring','oc_recurring_description','oc_return','oc_return_action','oc_return_history','oc_return_reason','oc_return_status','oc_review','oc_setting','oc_stock_status','oc_store','oc_tax_class','oc_tax_rate','oc_tax_rate_to_customer_group','oc_tax_rule','oc_tg_tglite_revolution_slider','oc_upload','oc_url_alias','oc_user','oc_user_group','oc_voucher','oc_voucher_history','oc_voucher_theme','oc_voucher_theme_description','oc_weight_class','oc_weight_class_description','oc_zone','oc_zone_to_geo_zone']

我无法发现我出错的地方。也许我会在不同的服务器上尝试下一步。

2 个答案:

答案 0 :(得分:3)

不要手动创建您的JSON字符串,只需使用json_encode,这就是为什么此函数仍然存在:

首先收集容器内的所有表名,完成收集,循环和整个位,完成后再进行编码。

$result = $conn->query($tablesGetSQL);
$JSONStringArray = array(); // initialize container       
while($row = $result->fetch_assoc()) {
    $JSONStringArray[] = $row['table_name']; // push all table names
}
// finally, encode
$JSONStringArray = json_encode($JSONStringArray);

$conn->close();
return $JSONStringArray;

答案 1 :(得分:1)

由于这一行,你得到两份副本

$JSONStringArray .= substr($JSONStringArray, 0, -1) . ']';

您执行正在执行子操作的.=,并将其添加到原始文件中,您应该执行=

即,应该只是:

$JSONStringArray = substr($JSONStringArray, 0, -1) . ']';

作为旁注,没有必要构建JSON字符串,PHP有一个内置的方法来生成json字符串。相反,你可以做

$data = array();
while($row = $result->fetch_row())
{
    $data += $row; // append returned array to the data array, since you wanted it future proof incase you added more columns to the SELECT query
}
// close connection
return json_encode($data);