I’m trying to write a simple script that goes over a file (name list) and would use each line (name) to produce an sql query to fetch the id of that name.
$linearray = explode("|", file_get_contents('designer.txt'));
foreach ($linearray as $line) {
$result = mysql_query("SELECT term_id FROM wp_terms WHERE slug = '{$line}'");
$row = mysql_fetch_assoc($result);
echo $row['term_id'];
}
Now, for some reason I only get the last word term_id. Although if I try to echo the line, it works.
Any idea why the sql is being execute only at the last line?
Don’t ever execute SQL queries in a loop! An SQL query is an expensive operation and you should keep them to a minimum. You’re much better off building the query to fetch all the data in one go.