2015-05-05 29 views
2

我從opencart安裝中選擇表名。 試圖將結果(單列)輸出爲字符串,如JSON數組。 這裏是代碼:來自mysql的雙輸出在php中

<?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個「[」,但我只有一次將它設置爲字符串。第一行也沒有關閉']'。

輸出:

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'] 

我不能當場在那裏我已經錯了。也許我會在下一個不同的服務器上嘗試它。

+1

沒有'foreach'循環內'while while循環 – Krishna38

+0

是啊只是爲了將來打樣。因爲只有1列,它應該循環一次? @ user3454479 – Sarfaraaz

+0

嘗試註釋掉第二個回顯gettablenames –

回答

1

你得到,因爲這條線的兩個副本

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

你這是做的SUBSTR,並將其添加到原來的.=,你應該做一個=

即應該是:

$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); 
+0

啊,那裏是它找到它感謝。愚蠢的錯誤-_-。作爲一個附註,$ data + = $ row的工作方式與$ data。= $ row相同。 – Sarfaraaz

+1

不,'。='是一個簡寫字符串連接,像我用它的'+ ='是連接到數組的簡寫形式 –

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; 
+0

會給出一個鏡頭並回復給你 – Sarfaraaz

+0

好吧,工作得很好,謝謝,但它沒有解釋在字符串處理期間有2'[''的原因。 @CJ Wurtz發現它 – Sarfaraaz

+0

@Sarfaraaz說不必要的連接'。='把你當前的字符串加倍了,但我絕不會建議採取這樣的路線,不要嘗試手動做,它容易出錯,只要讓'json_encode'工作,它更容易,無論如何,很高興這有幫助 – Ghost