SQL query using a php variable
Posted: 13 October 2008 04:12 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  50
Joined  02-28-2008

Hi

I need to do a query to retrieve the entry_id and weblog_id based on the title which comes from a loop and is stored in a php variable.

The query does not return any results, what I am doing wrong????

here is my code:

$rel_art_array = explode ("<br />", "{embed:Rel_Art}");
if (
$rel_art_array !="") {
  
for ($i = 0; $i <count($rel_art_array); $i++){
    $relation
= $rel_art_array[$i];
echo
$relation;

?>

{exp
:query sql="SELECT entry_id, weblog_id FROM exp_weblog_titles WHERE title ='<?php echo $relation; ?>' "}
{entry_id}
<br />


{/exp:query}

<?php     


   }
}
?>

Thanks in advance smile

Profile
 
 
Posted: 13 October 2008 06:41 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

Make sure php is being parsed on input- it needs to be for the above to work.  To see what’s really going on, might turn ‘Show queries’ on in ‘Admin- System Prefs- Output and debug’.

And when I’m doing something like this- I start by hard coding it- make sure it will work.

Lastly- I wouldn’t loop it like this.  I’d take the query out of the loop, hard code a single query that would work: WHERE title IN (‘x’, ‘y’, ‘z’)- though instead of using titles, I’d try passing entry_ids instead.  Anyway ssee if you can hard code it to work w/a single query.  Then format your variables to match the hard coded bit- echo that and run the query once.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 13 October 2008 08:09 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  122
Joined  08-05-2008

Debug the script by echo:ing $relation and see if that is what’s causing the problem.

 Signature 

———-
http://douglasstridsberg.com
My blog with random rants and lulz.

Profile
 
 
Posted: 13 October 2008 11:09 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  50
Joined  02-28-2008

$relation is ok, the loop gives the expected result but the query does not retrieve the right information, even when I put it outside the loop.

{exp:query sql=“SELECT entry_id, weblog_id, title FROM exp_weblog_titles WHERE title=‘My title’”}
<li><a href=”{path=”>{title}</a></li>
{/exp:query}

I get a different weblog id and title and not the one that refers to the query

Profile
 
 
Posted: 13 October 2008 11:12 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  122
Joined  08-05-2008

You know you could do all that query:ing with a weblog:entries tag?

And btw, I couldn’t help but notice that you’ve missed a closing curly bracket after the path= thing.

 Signature 

———-
http://douglasstridsberg.com
My blog with random rants and lulz.

Profile
 
 
Posted: 14 October 2008 03:09 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  50
Joined  02-28-2008

Thank you,

I tried a weblog:entries before, but it didn’t work. Something like this:

{exp:weblog:entries}
{if title ==“My entry”}

<li><a href=”{path=”>{title}</a></li>
{/if}
{/exp:weblog:entries}

But it does not work. Outside the loop I don’t get any results and inside the loop I get the same entry repeated several times.I guess it is looping over all the entries.

Also, category_id is not there.

It may be simple but still, I don’t see the way to do it.

Thanks

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64912 Total Logged-in Users: 30
Total Topics: 81864 Total Anonymous Users: 23
Total Replies: 440104 Total Guests: 185
Total Posts: 521968    
Members ( View Memberlist )
Newest Members:  bjmohrAqua193Bios Elementmjpoteetguimogranwelshmrcfthenetmonkeybenekwhobutsb