PHP: Query URLs from JSON against MySQL Database and Print Results
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$urls = $data['urls'];
$existingUrls = array();
$nonExistingUrls = array();
foreach ($urls as $url) {
$sql = "SELECT * FROM your_table WHERE url = '$url'" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$existingUrls[] = $url;
} else {
$nonExistingUrls[] = $url;
}
}
echo "Existing URLs:\n";
print_r($existingUrls);
echo "Non-existing URLs:\n";
print_r($nonExistingUrls);
$conn->close();
?>
Replace 'your_username', 'your_password', 'your_database', 'your_table' with your actual MySQL credentials and table name.
This code will connect to your MySQL database and query the URLs from the JSON against the 'url' column in the specified table. It will then separate the URLs into two arrays - 'existingUrls' for the URLs found in the database and 'nonExistingUrls' for the URLs not found in the database. Finally, it will print the results using print_r.
原文地址: https://www.cveoy.top/t/topic/p4Cc 著作权归作者所有。请勿转载和采集!