我有一个名为 MyArray 的数组,我想将其数据插入 mysql 但首先需要检查它是否已经存在于 mysql 中。谁能告诉我如何循环遍历数组以及如何引用数组中的每个变量然后将这些数据插入到 mysql 中?
$MyArray[] = array(
"usernameVar" => htmlspecialchars($usernameVar),
"profPic" => htmlspecialchars($profPic),
"idVar" => htmlspecialchars($idVar),
"standardResolution" => htmlspecialchars($standardResolution),
"imagePageLink" => htmlspecialchars($imagePageLink),
"createdTimeVal" => htmlspecialchars($createdTimeVal),
"imageTags" => htmlspecialchars($imageTags),
);
$MyArray = array_reverse( $MyArray );
//now i need to loop through array and insert non duplicate data in to mysql
$result = mysql_query("SELECT imageUrl FROM mytable WHERE imageUrl = '$standardResolution'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result) == 0) {
$j++;
echo "<a href='".$imagePageLink."'><img src='".$standardResolution."' width='150' height='150' border='0'></a><br> ";
// row not found, do stuff...
echo "New Users(".$i."):";
echo "<br>User Name(".$i."):".$usernameVar;
$result2 = mysql_query("INSERT INTO mytable (ID, username, profile_picture, instaId, imageUrl,imagePageURL, CreatedTime, imageTags, date) VALUES('$ID','$usernameVar','$ProfilePicVar','$idVar','$standardResolution','$imagePageLink','$createdTimeVal','$imageTags',NOW())");
if (!$result2) {
die('Invalid query: ' . mysql_error());
}
}
else
{
$m++;
// do other stuff...
echo "Already Added Users(".$i."):";
echo "<br>User Name(".$i."):".$usernameVar;
};