Solved

Need help with mysql statement rewrite

Posted on 2009-06-26
2
191 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now