Solved

Need help with mysql statement rewrite

Posted on 2009-06-26
2
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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