Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Need help with mysql statement rewrite

I've pieced together the script below and it almost works (the OR is a problem, as details in the comments below).

I strongly suspect that it can be rewritten to be much more efficient, maybe by combining the mysql call into one, or by handling the get info, set it up, and get info again type thing, differently.

Can you fix this so the OR problem is resolved and it's more efficient?  Thanks,  Chris

using php 5, mysql 5

----------------------------------------------------

$topic = "siteName LIKE '%top stories%'";

$result = mysql_query("SELECT id FROM mnc_sources WHERE $topic")  or die(mysql_error());
$source_ids = '';
if (mysql_num_rows($result)!= 0) {
 while($row = mysql_fetch_assoc($result)) {
  $source_ids .= 'sourceID = '.$row['id'].' OR ';  
//that OR is causing a problem, because it leaves a last OR in the WHERE section
 }
}


//now get rows with those ids


$result = mysql_query("SELECT * FROM mnc_topics WHERE $source_ids ORDER BY date DESC")  or die(mysql_error());
$contents = '';
if (mysql_num_rows($result)!= 0) {
 while($row = mysql_fetch_assoc($result)) {
 
  $contents .= '<br><a href="'.$row['link'].'">'.$row['title'].' - '.$row['date'].'';

 }
}
else {
  $contents .= "<BR>report temporarily unavailable";
}
$contents .= "";

echo $contents;
0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
1 Solution
 
Roger BaklundCommented:
To fetch all in one query, try this:

select * from mnc_topics,mnc_sources where mnc_sources.id=mnc_topics.sourceID and $topic

If it is still slow, it is because mnc_sources.id or mnc_topics.sourceID does not have an index.
0
 
St_Aug_Beach_BumAuthor Commented:
Hey, that works!

Thank you!

Chris
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now