我正在尝试使用 PHP 将数组插入 MySQL。我遵循这里给出的优秀建议来使用 implode 命令,它对一个阵列非常有效,但这个阵列似乎正在死去。这个数组与另一个略有不同,但我不知道如何解释这种差异。
这是我的代码:
$sql = array();
foreach( $ride_detail as $row ) {
$sql[] = '('.$row['id'].', "'.mysql_real_escape_string($row['name']).'",
"'.$row['version'].'")';
}
mysql_query('INSERT IGNORE INTO ride (ride_id, name, version) VALUES '.implode(',', $sql));
我一遍又一遍地收到这条消息。
Warning: Illegal string offset 'id' in ride_details.php on line 60
Warning: Illegal string offset 'name' in ride_details.php on line 60
Warning: Illegal string offset 'version' in ride_details.php on line 61
我的数组的内容(使用 print_r)是:
Array ( [id] => 21570117 [name] => 晚上下班回家 [start_date_local] => 1347302039 [elapsed_time] => 53:56 [moving_time] => 52:04 [distance] => 12.6 >>[average_speed ] => 14.5 [elevation_gain] => 474 [location] => 恩格尔伍德,CO [start_latlng] => 阵列([0] => 39.547792011872 [1] => -104.86300536431)[end_latlng] => 阵列([0] = > 39.655485888943 [1] => -104.88656991161 ) [版本] => 1355428869 [运动员] => 数组 (>>[id] => 832001 [名称] => Bob Kratchet [用户名] => bob_kratchet) [自行车] =>数组([id] => 281303 [name] => Giant Allegre commuter)[maximum_speed] => 29.3 [calories] => 372 >[average_power] => 107 [commute] => 1)
我是一个完全的菜鸟...