将关联数组插入mysql

时间:2013-01-17 08:45:24

标签: mysql arrays

我在数组中有以下数据,我想将每个数组索引的内容存储到mysql表myforum中:

    Array ( 
            [0] => zero, those players that are trialing for hib team, (hpb's) most of them are like 300 -400 pingers? umm..mager lagg and even worse when they play on uk server's i bet/ 
            [1] => 1998-04-25T213200Z 
            [2] => http//boards.ie/vbulletin/showpost.php?p=67075 
          ) 

mysql表colums是:content,date,post

[0] index value should store in content column,
[1] should store in date column and
[2] should store in post column

2 个答案:

答案 0 :(得分:1)

我没有发现你的代码有什么问题,只是逃避你留下的值,可能是因为你无法将数据插入表中。您可以尝试以下代码。

$con = mysql_connect("localhost","sufi","1234"); 
 if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 

     } 
 mysql_select_db("test", $con); 
 $xml = simplexml_load_file("boards.xml"); 
 $products[0] = (string)current($xml->xpath("/sioctBoardPost/sioccontent")); 
 $products[1] = (string)current($xml->xpath("/sioctBoardPost/dctermscreated")); 
 extract($products); 
 mysql_query("INSERT INTO forum (txt_content,txt_date,txt_about) 
 VALUES ('".mysql_escape_string($products[0])."','".mysql_escape_string($products[1])."','".mysql_escape_string($products[2])."')"); ?> 

答案 1 :(得分:0)

尝试使用以下示例:

<?php
$input_array = array('zero, those players that are trialing for hib team, (hpb\'s) most of them are like 300 -400 pingers? umm..mager lagg and even worse when they play on uk server\'s i bet','1998-04-25T213200Z','http//boards.ie/vbulletin/showpost.php?p=67075');
$input_array_count= count($input_array);
$coumn_array = array('content','date','post');
$input_array_implode="'".implode("','",$input_array)."'";
$coumn_array_implode="'".implode("','",$coumn_array)."'";
$insert_query=mysql_query("insert into tablename (".$coumn_array_implode.") values (".$input_array_implode.")") or die(mysql_error());
?>

我认为这可以帮助您解决问题。