Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-05-2007, 11:00 AM   #1 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
Query from multiple tables

Hi all,
I know this is super easy, but I'm a designer mostly. Trying to learn something here

I'm writing this script for the band I'm in with my church. I want to write something that will look to see who's playing and send them a text message. I have most of it worked out, but can't seem to get the initial query working. Anything look immediately wrong with this?

Thanks in advanced for any tips!:

Code:
<?
// database connect function
mydbconnect();
// setup date
$today = date("Ymd");
$row_count = 0;
$color1 = " class=\"altrow\"";
$color2 = "";

if ($orderby=="") $orderby = "dates.dateDate";
	
	// Start query
	$result = mysql_query("
		SELECT *
		FROM roster, dates, members
		WHERE (
			(roster.rosterMemberID = members.memberID)
		AND
			(roster.rosterDateID = dates.dateID)
		
			)
		ORDER BY 
		".$orderby." DESC") or die ("no DB connection<br /><br />".mysql_error() );
	$number = mysql_numrows($result);
	
	if ($number < 1){print "<tr><td class=\"altrow\" colspan=\"7\">There are no items available at this time.</td></tr>";}
	else {
		echo "
		<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" class=\"table1\" width=\"540\">
		<tr>
			<th>Service</th>
			<th>Who's On</th>
			<th>Send a reminder</th>
		</tr>";
		
		while($r=mysql_fetch_array($result))
		{    
			// ESTABLISH FIELD NAMES
			$rosterID=$r["roster.rosterID"];
			$rosterDate=$r["dates.dateDate"];
			$firstname=$r["members.memberFname"];
			
			
			// Date formatting from yyyy-mm-dd
			$year = substr($dateDate, 2, 2); 
			$month = substr($dateDate, -5, 2);
			$day = substr($dateDate, 8, 9); 
			$formatted_date = $month ."/". $day ."/". $year;
			
			// This is the code for alternating row colors
			$row_color = ($row_count % 2) ? $color1 : $color2;
			
			// loop here
			echo "
				<tr>\n
					<td>".$rosterDate."</td>\n
					<td>".$firstname."</td>\n
					<td><img src=\"images/button.gif\" /></td>\n
				</tr>\n";
			
			$row_count ++;
		}
		echo "</table>";
	}

?>
draven77 is offline   Reply With Quote
Old 08-05-2007, 11:17 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
What happens when you perform the query?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-05-2007, 11:34 AM   #3 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
It doesn't put anything into the <td> cells at all. so it looks like the query's logic might just be off.

I'm storing users in "members"
playing dates in "dates"
and "roster" holds all the joining IDs
draven77 is offline   Reply With Quote
Old 08-05-2007, 11:46 AM   #4 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
I should add that I've only written JOIN queries like this in MSSQL. This is my first attempt at mySQL queries with multiple tables.
draven77 is offline   Reply With Quote
Old 08-05-2007, 01:24 PM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
I use the MDB2 package when I work with databases in PHP, so I'm not terribly familiar with the included MySQL functions, but it appears that if the query fails mysql_query() returns false. Check to see if $results is false, and if it is, get the error message from MySQL by using mysql_error(). That should give you a clue as to why it's failing.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-05-2007, 06:27 PM   #6 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
I have this line in there to check for that I believe.
Code:
 or die ("no DB connection<br /><br />".mysql_error() );
draven77 is offline   Reply With Quote
Old 08-05-2007, 10:17 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
do you have access to a mysql manager like phpmyadmin? if so, print out your sql and run it in phpmyadmin. it should point you to the error or at least show you that you are getting blank results.

Code:
$sql = "
		SELECT *
		FROM roster, dates, members
		WHERE (
			(roster.rosterMemberID = members.memberID)
		AND
			(roster.rosterDateID = dates.dateID)
		
			)
		ORDER BY 
		".$orderby." DESC";
echo $sql;
exit;
now looking at the query, i don't see any where clause which specifies a date. as i understand it, this query would return all rows in your database.

i don't know your db structure well, but you could always try a left join:
Code:
SELECT *
		FROM roster
LEFT JOIN members on members.memberID=roster.rosterMemberID
LEFT JOIN dates on dates.dateID=roster.rosterDateID
		ORDER BY 
		".$orderby." DESC";
so now, if i'm on the right track anyway, we would want to add a clause to only select values with a specific date. unfortunately i have no idea what field it comes from. you have a variable: $dateDate in your code which is never assigned, yet you're using substr on it.

you need to take this down to smaller chunks and then put it together. work on a good query, then work on the logic to print it to the screen.
__________________
Mike
sde is offline   Reply With Quote
Old 08-06-2007, 04:45 PM   #8 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
Did that, and got my result!

So I must be setting up the returned variables incorrectly.

Code:
// ESTABLISH FIELD NAMES
			$rosterID=$r["roster.rosterID"];
			$rosterDate=$r["dates.dateDate"];
			$firstname=$r["members.memberFname"];
draven77 is offline   Reply With Quote
Old 08-06-2007, 04:48 PM   #9 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
Awesome, got it =) I didn't need the "tablename." in front of the returned value. Changed to:

Code:
$rosterID=$r["rosterID"];
draven77 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do Some Calculations on Query Results transfield PHP 9 03-06-2007 12:07 PM
Need help optimizing a query Belisarius Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 3 12-23-2005 06:33 AM
Order Multiple Items BUFFY PHP 12 10-20-2005 08:14 AM
Inserting the results of a select query into seperate tables Ste_Boro PHP 1 02-17-2005 05:05 AM
a query type of questiton sde Program Design and Methods 6 08-21-2003 04:55 PM


All times are GMT -8. The time now is 06:58 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting