php - 对于 Mysql 和 Oracle 表,从 PHP 中的变量将多行插入表中

标签 php mysql oracle variables magento

我正在从一些表中获取数据并将其存储在如下变量中:

$result = mysql_query("SELECT * FROM sample where column=mysql_insert_id()");
while($row=mysql_fetch_array($result))
{
   $str = "'". $row["name"] . "',". "'" . $row[quantity] . "'," . "'" . $row["id"];
}

因此,在我的变量 $str 中,假设我有以下值:

 shirt,10,1,pant,50,2....i.e. it will store values in a comma separated format.

现在我想将这些值插入到另一个表中,例如 test-:

$qry = "INSERT INTO test(name,quantity,id)values(".$str.");

现在我想将测试表中的值存储在两行中,例如:

shirt    10   1
pant     50   2

那么如何对 Mysql 和 Oracle 表执行相同的操作?

请帮忙

请参阅我的以下查询-:

    $query2 = "SELECT sfoi.name, sfoi.sku, sfoi.qty_ordered, sfoi.price, sfoi.row_total, sfo.base_subtotal, sfo.base_shipping_amount, sfo.base_grand_total,
    (select mso.order_primary from mysql_sales_order mso where mso.increment_id =sfo.increment_id) 
    FROM sales_flat_order sfo
    JOIN sales_flat_order_item sfoi
    ON sfoi.order_id = sfo.entity_id
    WHERE sfo.increment_id = ". $order_id ;
$result_query2 = mysql_query($query2);

因此,对于一个订单 ID,即一个订单可能包含超过 1 个产品,即许多名称、SKU、订购数量等。因此,在 mysql_fetch_array() 时,我希望所有产品数据都在一个变量中...我的获取代码如下:

while($row = mysql_fetch_array($result_query2))
{
$string = "'". $row["name"] . "',". "'" . $row["sku"] . "'," . "'" . $row["qty_ordered"] . "',". "'" . $row["price"] . "'," . "'" . $row["row_total"] . "'," . "'" . $row["base_subtotal"]. "'," . "'" . $row["base_shipping_amount"] . "'," . "'" . $row["base_grand_total"] ."'," . $row["prod_foreign"];
$query3 = "INSERT into mysql_sales_product(name, sku, qty_ordered, price, row_total, base_subtotal,base_shipping_amount,base_grand_total,prod_foreign) VALUES(".$string.")";

} 
$result_query_product_outbound = mysql_query($query3);

在这里,我想将 mysql_ fetch_array 的结果存储在变量中,这样如果有多行,我仍然可以使用变量 $string 传递这些行。例如-:

name   sku qty_ordered  price  row_total  subtotal shipping_amnt grand_total prod_foreign            
nokia   nk      2        500      1000      1000      300            1300         11
sansung sam     3        400      1200      1200      500            1700         30
sony    sny     4        200       800       800      200             1000         45

最佳答案

对于 Oracle,如下所示:

INSERT ALL
   INTO your_table(column1,column2,column3) VALUES('values1.1', 'values1.2', 'values1.3')
   INTO your_table(column1,column2,column3) VALUES('values2.1', 'values2.2', 'values2.3')
SELECT * FROM dual;

关于php - 对于 Mysql 和 Oracle 表,从 PHP 中的变量将多行插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9767753/

相关文章:

php - 如果变量是 int 值,array_combine php 函数不会保留字符串变量的数据类型

javascript - 如何将 css 样式表和 js 文件调用到自定义 WordPress 主题中

syntax - MYSQL 查询 : extract all records that have SSCF in the LicenseNumber

mysql - Rake 似乎在劫持 MySQL

mysql - 将文件内容加载到表格的特定单元格中

php - 将 cURL 请求的结果转换为表单

php - 将数据库查询结果格式化为 JSON 数组并读入表单

mysql - varchar2 和 number 数据类型导致 phpmyadmin 中的 SQL 查询编辑器出现错误

sql - RegExp 查找和替换忽略部分匹配

SQL 标识符无效?