For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > February 2007 > SQL Query - Using variable from another SQL Query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SQL Query - Using variable from another SQL Query
Matthew Ferry

2007-02-12, 6:59 pm

Hello Everyone....

Got a simple / stupid question.
Worked on this all night. I'm over looking something very basic here.

The query "event_time" brings back the calendar id for each event that is pending in the future.
ie.... 12, 13, 14, 26 (There could be 100 of them out there)

The second query "events" needs to meet both reqirements.
1 - cal_category='501'
2 - cal_id= a number from the "event_time" query

I think i need to do a loop inside of a loop

Thanks...

Matt


Here is my code:

<?php

$todays_year = date("Y");

$todays_month = date("m");

$todays_day = date("d");

$tstamp = mktime(0, 0, 0, $todays_month, $todays_day, $todays_year);

$event_time = mysql_query("SELECT cal_id FROM egw_cal_dates where cal_start > $tstamp", $db);

$events = mysql_query("SELECT * FROM egw_cal WHERE cal_category='501' and cal_id='$event_time'\n", $db);



if ($event = mysql_fetch_array($events)) {

echo "<center>\n";

echo "<HR>\n";

do {

echo "<B><Font Size='10'>$event[cal_title]    -   $event[cal_location]</B></Font>\n";

echo "<BR>\n";

echo "$event[cal_description]";

echo "<BR>\n";

echo "<HR>\n";

} while ($event = mysql_fetch_array($events));

} else {

echo "No Public Events Are Currently Scheduled...";

}

?>


Mike Morris

2007-02-13, 3:58 am


I think you don't need to break this into two queries... this is really a SQL question,
not a PHP question...

Just do a "join" on the two tables:
* where table1.cal_id = table2.cal_id

and then have a where clause that does all your filtering:

* and table1.Date > now() and table2.cal_category='501'


Using SQL instead of code is almost always the right thing to do. Learning how to
do more sophisticated queries will pay off big time in the long run... and good
tutorials are widely and freely available...



From: "Matthew Ferry" <mferry@retailautomation.biz>
To: <php-db@lists.php.net>
Date sent: Mon, 12 Feb 2007 11:14:32 -0500
Subject: SQL Query - Using variable from another SQL Query

> Hello Everyone....
>
> Got a simple / stupid question.
> Worked on this all night. I'm over looking something very basic here.
>
> The query "event_time" brings back the calendar id for each event that is pending in the future.
> ie.... 12, 13, 14, 26 (There could be 100 of them out there)
>
> The second query "events" needs to meet both reqirements.
> 1 - cal_category='501'
> 2 - cal_id= a number from the "event_time" query
>
> I think i need to do a loop inside of a loop
>
> Thanks...
>
> Matt
>
>
> Here is my code:
>
> <?php
>
> $todays_year = date("Y");
>
> $todays_month = date("m");
>
> $todays_day = date("d");
>
> $tstamp = mktime(0, 0, 0, $todays_month, $todays_day, $todays_year);
>
> $event_time = mysql_query("SELECT cal_id FROM egw_cal_dates where cal_start > $tstamp", $db);
>
> $events = mysql_query("SELECT * FROM egw_cal WHERE cal_category='501' and cal_id='$event_time'\n", $db);
>
>
>
> if ($event = mysql_fetch_array($events)) {
>
> echo "<center>\n";
>
> echo "<HR>\n";
>
> do {
>
> echo "<B><Font Size='10'>$event[cal_title]    -   $event[cal_location]</B></Font>\n";
>
> echo "<BR>\n";
>
> echo "$event[cal_description]";
>
> echo "<BR>\n";
>
> echo "<HR>\n";
>
> } while ($event = mysql_fetch_array($events));
>
> } else {
>
> echo "No Public Events Are Currently Scheduled...";
>
> }
>
> ?>
>
>




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com