Solved

Need help with mysql statement rewrite

Posted on 2009-06-26
2
182 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
Comment Utility
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
Comment Utility
Hey, that works!

Thank you!

Chris
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

10 Experts available now in Live!

Get 1:1 Help Now