English 中文(简体)
加入MySQL表
原标题:Insert Array into MySQL Table

我是新到的,我正试图说明如何将以下阵列列入一个叫做“skus”的MYSQL表格。

每个分支都是一个库。

补贴的每个价值都是补贴。 (例如蓝色、绿色、黄等)

因此,我想谈谈这一点。

ku 食

ku是表上的主要关键,在我增加选择价值时,会实现自动化。

Array
 (
     [0] => Array
         (
             [2] => 27
             [3] => 17
         )

     [1] => Array
         (
             [2] => 28
             [3] => 17
         )

     [2] => Array
         (
             [2] => 27
             [3] => 18
         )

     [3] => Array
         (
             [2] => 28
             [3] => 18
         )

 )
问题回答

因此,你的阵列是:

$skus=array(
    [SKU] => Array(
          option  => 27,
          option2  => 17
     )
);

一俟您创立了我的SQL表,你就可以利用以下方法,将阵列和投入表:

$connect = mysql_connect(HOST,USER,PASS) or die(mysql_error());

$delim = ":";

$length = count($skus);

foreach ($skus as $sku => $opt_array) {

    $i = 0;
    $opts =   ;

    foreach ($opt_array as $option_id) {
        if ($i == count($opt_array) {
            $opts .= $option_id;
        } else {
            $opts .= $option_id . ",";
        }
        $i++;
    }

    $query = "INSERT INTO `skus` (sku,opt_id,opt_id2) VALUES ( " . $sku . " , " . $opts . " )";

    mysql_query ($query, $connect) or die (mysql_error());
}

As you say you only have two columns in your table your Option ID s will have to live in the same field within your table. This is why I have declared $delim as the delimeter between option ID s.

你通过阵列对第一个通道的实际询问是:

INSERT INTO `skus` (sku,opt_id,opt_id2) VALUES ( SKU , 27 ,  17 )

等等。

希望这一帮助。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签