I use the php operator && to select multiple data so that there is no duplication on mysql. Does the code that I use below run fine? Is there a more simple use of PHP operators?
$date= date('Y/m/d'); $cekcount = mysql_num_rows(mysql_query("SELECT * FROM `pending_media` where `mediaid`='$dielz'")); $cekcount2 = mysql_num_rows(mysql_query("SELECT * FROM `media` where `mediaid`='$dielz'")); $selectcount = mysql_query("SELECT * FROM `media` where `date`='$date' AND `uplink`='$nam'"); $cekcount3 = mysql_num_rows($selectcount); if($cekcount == 0 && $cekcount2 == 0 && $cekcount3 == 0){ mysql_query("INSERT INTO pending_media VALUES('','$nam','$dielz')");
Advertisement
Answer
Upgrade to mysqli, I’ll recommend object-oriented syntax because it is nicer to work with.
In accordance with the best practice of “minimize calls to the database”, I’ll condense your three queries into a single, united SELECT call then check for a non-0 result.
My untested suggestion using mysqli’s object-oriented syntax (I did test the SELECT query in PHPMyAdmin):
$query = "SELECT SUM(tally) FROM ( SELECT COUNT(*) AS tally FROM pending_media WHERE mediaid = ? UNION ALL SELECT COUNT(*) FROM media WHERE mediaid = ? OR (date = ? AND uplink = ?) ) t"; $conn = new mysqli("localhost", "root","","dbname"); $stmt = $conn->prepare($query); $stmt->bind_param("ssss", $dielz, $dielz, $date, $nam); $stmt->execute(); $stmt->bind_result($tally); $stmt->fetch(); if (!$tally) { $stmt->close(); // insert qualifying data $stmt = $conn->prepare("INSERT INTO pending_media VALUES ('',?,?)"); $stmt->bind_param("ss", $nam, $dielz); if ($stmt->execute()) { echo "Insert Query Error"; // $stmt->error; }else{ echo "Success"; } }