Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-09-2006, 03:51 PM
WebProWorld Pro
 

Join Date: Aug 2004
Location: Maryland
Posts: 219
webmasterjunkie RepRank 0
Default PHP MySQL Selection Question

Hello All,

I am trying to check my MySQL tables, which all have a common field of user_id. I have searched on the net and tried tons of diffrent things.

Unfortunately I am stuck. I will post some of the core code of what I have below. If anyone has a better way of doing this, please let me know. I am not too familiar with JOIN or such.

Code:
<?php
	$db_connection = @mysql_connect($db_host, $db_user, $db_pass) or die (mysql_error());
	@mysql_select_db($db_name, $db_connection) or die (mysql_error());
	$sql = "SELECT * FROM $course_table, $event_table, 
	$job_table, $housing_table, $news_table, $profiles_table, $products_table, $research_table, 
	$reviews_table, $services_table, $travel_table, $vehicles_table, $wanted_table 
	WHERE $course_table.user_id = '$user_id' OR $event_table.user_id = '$user_id' 
	OR $job_table.user_id = '$user_id' OR $housing_table.user_id = '$user_id' 
	OR $news_table.user_id = '$user_id' OR $profiles_table.user_id = '$user_id'	
	OR $products_table.user_id = '$user_id' OR $research_table.user_id = '$user_id' 
	OR $reviews_table.user_id = '$user_id' OR $services_table.user_id = '$user_id' 
	OR $travel_table.user_id = '$user_id' OR $vehicles_table.user_id = '$user_id' 
	OR $wanted_table.user_id = '$user_id'";
	$result = @mysql_query($sql, $db_connection) or die(mysql_error());
	$total_items = mysql_num_rows($result);
	mysql_close($db_connection);	
?>
Reply With Quote
  #2 (permalink)  
Old 03-10-2006, 10:31 PM
bodgekaloopie's Avatar
WebProWorld Member
 

Join Date: Aug 2004
Location: Michigan, USA
Posts: 67
bodgekaloopie RepRank 0
Default

What is the error or failure you are experiencing?
You're checking your tables specifically for what?
Are you echoing results once the query is performed?

If there is more code in your document, please post all of it or we won't be able to fully grasp what it is you are trying to achieve so that we can help you.
Reply With Quote
  #3 (permalink)  
Old 03-10-2006, 11:29 PM
DrTandem1's Avatar
WebProWorld 1,000+ Club
 

Join Date: Oct 2003
Location: Encinitas, CA
Posts: 1,908
DrTandem1 RepRank 2
Default

Yes, without knowing what you are trying to do with your script it's only a guess. If you are trying to troubleshoot this, I would add echo statements so you can see what is happening.
__________________
DrTandem's San Diego Web Page Design, drtandem.com
Reply With Quote
  #4 (permalink)  
Old 03-11-2006, 02:10 AM
WebProWorld Member
 

Join Date: May 2004
Location: Phuket, Thailand
Posts: 35
Tjaart RepRank 0
Default

It looks like you are trying to 'merge' the results from multiple queries into one result set, i.e.
select * from $course_table where $course_table.user_id = '$user_id'
UNION
select * from $event_table where $event_table.user_id = '$user_id'
etc

If this is correct then what you need is a UNION...

There are a few things to be aware of, but you can find what you need at http://dev.mysql.com/doc/refman/5.0/en/union.html

Let us know if that's not what you are trying to achieve.

Good luck
Reply With Quote
  #5 (permalink)  
Old 03-11-2006, 03:26 AM
WebProWorld Member
 

Join Date: May 2004
Location: Phuket, Thailand
Posts: 35
Tjaart RepRank 0
Default

Using a UNION result set in a webpage could be a mission, especially if you have that many tables, and you might be better of just doing individual queries to the server or maybe doing the JOIN if you want the information returned for many '$user_id's.

Depends what you want to achieve in the end.

http://dev.mysql.com/doc/refman/5.0/en/join.html

Have fun
Reply With Quote
  #6 (permalink)  
Old 03-13-2006, 12:55 PM
WebProWorld Pro
 

Join Date: Aug 2004
Location: Maryland
Posts: 219
webmasterjunkie RepRank 0
Default

I want to get how many items a user has posted in all tables of the database. Each table had a user_id field in it.

So a user can post 2 items in $job_table and 4 items in $housing_table. I want to be able to echo out that the user has 6 total items and what table they are in.

Sorry for not explaing it correctly. Also, I am not receiving any error, just keeps spitting out $total_items = 0 when I knwo there are some there. I just want this returned for one user_id which is based on a session.
Reply With Quote
  #7 (permalink)  
Old 03-13-2006, 03:03 PM
WebProWorld Member
 

Join Date: Feb 2005
Location: United States
Posts: 111
wsmeyer RepRank 0
Default

I know it is just part of the code but there isn't anything that sets a value for $user_id. If that is coming from another query echo that at the beginning of the script so you know it has a valid value.

On a side note, does the names of your tables really start with a $, I didn't even know that was possible.

William.
Reply With Quote
  #8 (permalink)  
Old 03-14-2006, 10:21 AM
WebProWorld Pro
 

Join Date: Aug 2004
Location: Maryland
Posts: 219
webmasterjunkie RepRank 0
Default

Quote:
Originally Posted by wsmeyer
I know it is just part of the code but there isn't anything that sets a value for $user_id. If that is coming from another query echo that at the beginning of the script so you know it has a valid value.

On a side note, does the names of your tables really start with a $, I didn't even know that was possible.

William.
The $user_id comes from a session:
Code:
$user_id = $cookie_info[0];
The table information is stored in a constants page:
Code:
$user_table = $db_pfix . "users";
$profiles_table = $db_pfix . "profiles";
etc...
Reply With Quote
  #9 (permalink)  
Old 03-14-2006, 01:02 PM
Easywebdev's Avatar
WebProWorld Veteran
 

Join Date: Apr 2004
Posts: 328
Easywebdev RepRank 1
Default

There is no need to select * as all you want is the occurances of the user_id in each table.

Use this.
Code:
<?php
   $db_connection = @mysql_connect($db_host, $db_user, $db_pass) or die (mysql_error());
   @mysql_select_db($db_name, $db_connection) or die (mysql_error());

   $tables = array($job_table, $housing_table, $news_table, $profiles_table, $products_table, $research_table,
   $reviews_table, $services_table, $travel_table, $vehicles_table, $wanted_table);
   
   $total_items = 0;
   foreach($tables as $table)
   {
   	$sql = "SELECT COUNT(user_id) AS num_entries FROM $table WHERE user_id='$user_id'";
        $result = @mysql_query($sql, $db_connection) or die(mysql_error());
        $row = mysql_fetch_row($result);
        $total_items += $row[0]['num_entries'];
   }
   echo $total_items;

   mysql_close($db_connection);   
?>
You should not be storing your table names as variables. In your constants file define them instead, ie define("PROFILES_TABLE",$db_pfix . "profiles");

The above code sends more queries but as its a simple COUNT of a single column and you dont have the huge WHERE clause it should be quicker.

Hope that helps.
Reply With Quote
  #10 (permalink)  
Old 03-23-2006, 12:50 PM
WebProWorld Pro
 

Join Date: Feb 2004
Location: England Baby!
Posts: 222
icb01co2 RepRank 0
Default

Easywebdev - Was about to post a solution similar till i read yours, I like your solution its clean and elegant. Thats all i wanted to say, lol.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , , ,



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

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


Search Engine Optimization by vBSEO 3.2.0