There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop mac malware motherboard mouse network networking outlook 2007 power printer problem ram restart router screen slow sound trojan usb virus vista vista 32-bit windows windows xp winxp wireless wmp
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
php/mysql help


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
Sportznow's Avatar
Junior Member with 9 posts.
 
Join Date: May 2004
Experience: Intermediate
03-Jun-2004, 11:12 PM #1
php/mysql help
Hello, I want to use php to sort through multiple tables in a mysql database and have them be in order by a certain field. However, I can't seem to get it to sort through multiple tables. As of now it prints out the multiple tables in order, but doesn't mix the results together in the tables. Here is the code:

<?
function db_connect(){
$hostname = "*****";
$username = "*****";
$password = "*****";
$dbName = "******";

mysql_connect($hostname, $username, $password) or die("DB connection unavailable");
mysql_select_db( "$dbName") or die( "Unable to select database");
}

function search_db($usertable){
//query details table begins
$sql = "SELECT * ";
$sql .= "FROM $usertable ";
$sql .= "ORDER BY `Player` ASC LIMIT 0, 30 ";

$result = mysql_query($sql) or die("Query failed");

while($row = mysql_fetch_array($result))
{
$variable1=$row["Player"];
$variable2=$row["Avg"];
$variable3=$row["HR"];
$variable4=$row["RBI"];
$variable5=$row["Runs"];
$variable6=$row["SB"];
$variable7=$row["CS"];
$variable8=$row["OBP"];
$variable9=$row["AB"];

print ("<tr>");
print ("<td>$variable1</td>");
print ("<td><div align=center>$variable2</div></td>");
print ("<td><div align=center>$variable3</div></td>");
print ("<td><div align=center>$variable4</div></td>");
print ("<td><div align=center>$variable5</div></td>");
print ("<td><div align=center>$variable6</div></td>");
print ("<td><div align=center>$variable7</div></td>");
print ("<td><div align=center>$variable8</div></td>");
print ("</tr>");
}
}
db_connect();
search_db('Yankees');
search_db('Anaheim');
search_db('Arizona');
?>

Can someone please give some help?
Snake~eyes's Avatar
Senior Member with 640 posts.
 
Join Date: Apr 2002
04-Jun-2004, 01:08 AM #2
So you want the data to sort it all together?


Info from Yankees, Anaheim and Airzona to all mix together in order?
deuce868's Avatar
Senior Member with 638 posts.
 
Join Date: Nov 2000
Location: MI
04-Jun-2004, 08:02 AM #3
Make sure that the player field is just like that. Caps on the first letter. Try leaving out the `. I never end up using them in php queries.

Now if you are trying to sort all three tables together by that field so that you would have Arizona players mixed with Yankee players all sorted by Player, you need to add some more code to take all three results and sort them manually with a manual sort function.
__________________
www.ricksweb.info
www.2webheads.com
Quote:
Techies just think a little differently...at least that's what they keep telling me.
Sportznow's Avatar
Junior Member with 9 posts.
 
Join Date: May 2004
Experience: Intermediate
04-Jun-2004, 11:47 AM #4
Yes, I want the results mixed all together. How would I go about doing this?
Snake~eyes's Avatar
Senior Member with 640 posts.
 
Join Date: Apr 2002
04-Jun-2004, 12:08 PM #5
Sounds to me like your table structure is wrong then. Why create a thing for each team? Just use the same structure as one of the teams and add an extra column that says team. Or better yet have a table with all the teams and cross reference the id into the other tables.
__________________
Epic Servers

Please do not email/pm for support, use the forums
deuce868's Avatar
Senior Member with 638 posts.
 
Join Date: Nov 2000
Location: MI
04-Jun-2004, 01:06 PM #6
you would have to do all your queries and put all of the results into a single giant array. Look up sort on php.net and there is a way to create a custom sort function. Once you have it sorted you will have to look through your entire array and print them out to get them mixed.
__________________
www.ricksweb.info
www.2webheads.com
Quote:
Techies just think a little differently...at least that's what they keep telling me.
Sportznow's Avatar
Junior Member with 9 posts.
 
Join Date: May 2004
Experience: Intermediate
04-Jun-2004, 09:51 PM #7
well, I have queries that use only certain teams, that is why I don't use one big table. I will look that up on PHP.Net, thankyou.
Guy's Avatar
Guy Guy is offline
Senior Member with 260 posts.
 
Join Date: Feb 1999
Location: abby, BC, canada
09-Jun-2004, 03:56 PM #8
I agree with snake~eyes that your table structure could be better defined, but if you want to use your existing structure, this should help. NOTE: you will need mysql 4.0 or higher and I havnt tested this at all so there my be some errors

PHP Code:
function search_db($tables)
{
  
$sql "";
  foreach( 
$tables as $usertable )
  {
    if( 
$sql != "" )
      
$sql .= "UNION ";
    
$sql .= "(SELECT * FROM $usertable ORDER BY `Player` ASC LIMIT 0, 30) ";
  }
  
$sql .= "ORDER BY `Player`";
    

  
$result mysql_query($sql) or die("Query failed");

  while(
$row mysql_fetch_array($result))
  {
    
$variable1=$row["Player"];
    
$variable2=$row["Avg"];
    
$variable3=$row["HR"];
    
$variable4=$row["RBI"];
    
$variable5=$row["Runs"];
    
$variable6=$row["SB"];
    
$variable7=$row["CS"];
    
$variable8=$row["OBP"];
    
$variable9=$row["AB"];

    print (
"<tr>");
    print (
"<td>$variable1</td>");
    print (
"<td><div align=center>$variable2</div></td>");
    print (
"<td><div align=center>$variable3</div></td>");
    print (
"<td><div align=center>$variable4</div></td>");
    print (
"<td><div align=center>$variable5</div></td>");
    print (
"<td><div align=center>$variable6</div></td>");
    print (
"<td><div align=center>$variable7</div></td>");
    print (
"<td><div align=center>$variable8</div></td>");
    print (
"</tr>");
  }
}

db_connect();
search_db( array('Yankees''Anaheim''Arizona') ); 
Snake~eyes's Avatar
Senior Member with 640 posts.
 
Join Date: Apr 2002
09-Jun-2004, 04:02 PM #9
Quote:
Originally Posted by Sportznow
well, I have queries that use only certain teams, that is why I don't use one big table. I will look that up on PHP.Net, thankyou.
If you have queries for certain teams all you do is do

Code:
SELECT * FROM teams WHERE teamname='$team'
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:22 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.