如何从多个字符串中提取信息并使用PHP将其作为单独的表输入到数据库中?

时间:2016-01-09 11:23:53

标签: php

我在下面列出了200多条相同的代码行:

1/5/2016    6   5   8   0   6       1    
Offset  1  
1/4/2016    2   7   9   2   3       18   
Offset  4  
1/3/2016    2   1   7   4   7       14   
Offset  6 

我想在上面的列表中使用PHP提取每个值,并将它们分别输入到数据库中,如下所示:

表1:

- Date: 1/5/2016    
- Val1: 6   
- Val2: 5   
- Val3: 8   
- Val4: 0   
- Val5: 6       
- Val6: 1    
- Offset:  1  

表2:

- Date: 1/4/2016    
- Val1: 2   
- Val2: 7   
- Val3: 9   
- Val4: 2   
- Val5: 3       
- Val6: 18   
- Offset:  4  

依此类推,有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

谢谢,我想通了!这是我的解决方案:

define('DB_HOST', 'localhost');
define('DB_USER', 'test');
define('DB_PASSWORD', 'test#');
define('DB_DATABASE', 'test');
===
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
//Select database
$db = mysql_select_db(DB_DATABASE);

$e = explode( 'X', $input1 );
foreach($e as $val) {
preg_match_all('!\d+!', $val, $matches);

$Date = $matches[0][0] . "/" . $matches[0][1] . "/" . $matches[0][2];
$Val1 = $matches[0][3];
$Val2 = $matches[0][4];
$Val3 = $matches[0][5];
$Val4 = $matches[0][6];
$Val5 = $matches[0][7];
$Val6 = $matches[0][8];
$MN = $matches[0][9];


$qry = "INSERT INTO PowerBall(Date, Val1, Val2, Val3, Val4, Val5, Val6, MN) 
VALUES('$Date','$Val1','$Val2','$Val3','$Val4','$Val5','$Val6','$MN')";
$result = @mysql_query($qry);