I have a piece of PHP code that is generating over 6000 queries to mySQL and I have the feeling this could be done a lot better…
$q0 = "SELECT id_productFROM products";
$e0 = mysql_query($q0) or die("Select failed: " . mysql_error());
while($r0 = mysql_fetch_array($e0)){
$id_product = $r0['id_product'];
$q1 = "SELECT * FROM productspeople WHERE id_product = $id_product ";
$e1 = mysql_query($q1) or die("Select failed: " . mysql_error());
if(mysql_num_rows($e1) == 0){ $count++; }else{ }
}
echo $count. "<br />";
My question is … Is there a better way of counting “non occurences” of a certaing id field of a table in another table? If so .. how?
Thanks in advance and thanks for understanding this (most probably) noobness of mine.
One query.
Oh, and it will help tremendously if there is an index on
productspeople.id_product.As a general tip, try to do as much work as possible on the database. In comparison it is ridiculously inefficient to send queries back and forth between PHP and the DB.