Solved

Need help with mysql statement rewrite

Posted on 2009-06-26
2
193 Views
Last Modified: 2012-05-07
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
Comment
Question by:St_Aug_Beach_Bum
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24726022
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
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 31597075
Hey, that works!

Thank you!

Chris
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question