我正在做一次性數據導入(所以開銷不是問題)從一箇舊表到Wordpress註釋表。我遍歷舊錶,使用php調整新表的一些數據,然後執行每個插入。它只會插入第一行。如果我再次運行代碼,它會插入相同的第一行,所以我不認爲有主鍵問題。MySQL插入循環只插入一行
require('wp-config.php');
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$result = mysqli_query($con,"SELECT *
FROM user_import u, wp_posts p, wp_postmeta m
WHERE p.ID = m.post_id
AND m.meta_key = 'fanfic_id'
AND m.meta_value = u.fanfic_id");
while($row = mysqli_fetch_array($result))
{
$nick=$row['user_nickname'];
$ip=$row['user_ip_address'];
$date=strToTime($row['user_fanfic_date']);
$date2=strftime('%Y-%m-%d 12:00:00',$date);
$gmt=strftime('%Y-%m-%d 12:00:00', $date);
$datemine = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));
$fanfic_id=$row['fanfic_id'];
$getPostID="SELECT post_id FROM wp_postmeta WHERE meta_key='fanfic_id' and meta_value=$fanfic_id";
$result2 = mysqli_query($con,$getPostID);
while($row2 = mysqli_fetch_array($result2)){
$post_id=$row2['post_id'];
}
$review=$row['user_fanfic_review'];
$sql="INSERT INTO wp_comments(comment_approved, user_id, comment_post_id, comment_author, comment_author_ip, comment_date, comment_date_gmt, comment_content)
VALUES(1, 0, $post_id,'$nick','$ip', '$date2', '$gmt', '$review') ";
$result = mysqli_query($con,$sql);
}
mysqli_close($con);
你爲什麼不使用wordpress數據庫對象'$ wpdb'? http://codex.wordpress.org/Class_Reference/wpdb – Konsole
'$ getPostID'查詢是否返回多行?因爲你在循環中獲取結果,但每次都覆蓋相同的變量。 – Barmar
我不知道那個對象。我得看看它。謝謝。 –