Page 1 of 1

displaying row based on an outside variable

Posted: Thu Nov 18, 2010 9:03 pm
by gopanthers
I think I'm just one step away from making this works but I need help in how to tie A & B together.

I've made a page within the WordPress structure. Since it's within WordPress, users will already be logged in and I am able to do things like display their username and other things. Without going through all the code, here's how I display the user's WordPress display name (which I will try to use later):

Code: Select all

get_currentuserinfo();
      echo 'User display name: ' . $current_user->display_name . "\n";
Even though I don't make any call to the WordPress database myself, lets call that database A. Then I have my own custom, single-table database that has no relation to WordPress at all. Lets call that database B. On this same page I created (within WordPress) I am able to call database B and display content from it. This database is a directory of member's data, with one row being for each person. One of those data fields is a copy of each person's WordPress display name so I can hopefully match up the two databases.

Code: Select all

$db = mysql_connect("localhost", "XXXX", "XXXX");
	mysql_select_db("DATABASENAME",$db);
	
	$query = "SELECT * FROM brotherhood ORDER BY lastname";
	$result = mysql_query ($query);
	
	if (@mysql_num_rows($result)) 
	{ 
	print "<table border=\"1\" >\n"; 
	print "<tr> 
		<td>First Name</td>
		<td>Last Name</td>
		<td>WordPress Display Name</td>
	</tr>\n"; 
	
	while($row = mysql_fetch_array($result)) { 	
	print "<tr>\n";  
	    print "<td>".$row['firstname']."</td>\n";    
	    print "<td>".$row['lastname']."</td>\n";    
	    print "<td>".$row['wp_user']."</td>\n";    
	    print "</tr>\n";
	}
	print "</table>\n"; 
	
	} else { echo "<p>Sorry, no records were found!</p>"; }
And this code above works just fine (although I shortened the real display to just three fields for simplicity). So on the same page, I am able to output the logged-in user's display name (from database A (WordPress)), and later on I can also output all data from database B (custom).

Now I just want to know simply how to use a variable from the first database to specify/limit the output of the second database. What I want to do is to display only the one row of data from my custom database B (like firstname, lastname, and wp_user) but only where "wp_user" matches the "$current_user->display_name" value of the logged-in user. I tried several times but nothing worked because I don't really know what I'm doing. Can anybody please help me figure out how to do this?

Re: displaying row based on an outside variable

Posted: Fri Nov 19, 2010 1:33 pm
by gopanthers
FYI, somebody helped me solve this.

Code: Select all

$query = "SELECT * FROM brotherhood where wp_user='$current_user->display_name'";