【发布时间】:2014-12-02 17:09:43
【问题描述】:
我有多个数组:
数组 (
[1] => Array
(
[0] => http://213b572-ba681bf9cc9e
[1] => http://f057-4139-ac40-bc4449722ffc
[2] => http://b-c151-4ba1-b7b7-842771c36d6b
[3] => http://5a77fb-8fce-4793-868f-c9fd73524037
)
[2] => Array
(
[0] => http://8-d832-4b34-a55b-da04ad8cdd09
[1] => http://b38-6a60-4233-b207-f40fae2ef431
[2] => http://3-f31c-49c4-87ee-fcada05a105f
[3] => http://07514-e438-45e2-906e-b440cbcbf8dc
)
......
[76] => Array
(
[0] => http://8-d832-4b34-a55b-da04ad8cdd09
[1] => http://b38-6a60-4233-b207-f40fae2ef431
[2] => http://3-f31c-49c4-87ee-fcada05a105f
[3] => http://07514-e438-45e2-906e-b440cbcbf8dc
)
我希望我的数据库是:
| id | id_page | url |
| 1 | 1 | http://jjjjjjjjj |
| 2 | 1 | http://jjjjjjjjj|
......
| 1000 | 76 | http://jjjjjjjjj|
我用:
foreach ($hrefs as $key => $href) {
mysqli_query($con, "INSERT INTO urls(`id`, `id_page`, `url`) VALUES ('', '$key', '$href')");
}
但不工作。
@:Aleksandr Mochalygin
mysqli_connect.php
`$con = mysqli_connect("localhost", "root", "", "vlbd");
mysqli_query($con, "set names 'utf8'");
//Check connection
if(mysqli_connect_errno()) {
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}`
功能查询:
function insertUrlsToDb() {
global $con;
$pages = getPagesFromDb(); // pages array to database (limit 4 pages)
$hrefs = getHrefByPages($pages); // hrefs multidimentions array
foreach ($hrefs as $href) {
foreach ($href as $key => $value) {
mysqli_query($con, "INSERT INTO urls(`id`, `id_page`, `url`)
VALUES ('', '$key', '$value')");
}
}
mysqli_close($con);
}`
$pages 是数据库中的数组(限制 4 页) $hrefs 是上面的多维数组。
【问题讨论】:
-
你看errors了吗?