Joining tables w/other conditions

Jeanette
2008-05-11 05:32:16
Joining tables w/other conditions
I am completely new to PHP. Right now, I have a form that is currently working, searching on one table. I've copied other scripts, and got some help from the boards. Now I'd like to join a 2nd table and I need some help getting it to work. My first table is named "phoneList" and I'd like to join it with a second table called "meaning". I will match on the field called "Lastname" and the fields I want to return are Firstname, Lastname, Email, and Phone from the phoneList table and a field called meaning from the meaning table (the thought is each last name has a different meaning). Right now, this is all test data for me to learn from. The form is at www.fatcreative.com/test.html and my current code is:

# GRAB THE VARIABLES FROM THE FORM
$Lastname = $_POST['Lastname'];
$Firstname = $_POST['Firstname'];


//this should select based on form selections
$query = "SELECT * FROM phoneList
WHERE ID > 0 ";

if (strlen($Firstname) > 0) {
$query .= "AND Firstname='$Firstname'";
}

if (strlen($Lastname) > 0) {
$query .= "AND Lastname='$Lastname'";
}

$result = mysql_query($query)
or die ("Couldnt execute query");

$numrows = mysql_num_rows($result); //count the number of rows returned
if($numrows < 1 ) //if none!!!
{
echo ("There were no rows returned from the database");
}
else //otherwise draw the table
//put info on new lines
echo "<table cellspacing='15'>";
echo "<tr><td colspan='3'></td></tr>";
while($row = mysql_fetch_assoc($result))
{
extract($row);
echo "
<tr>n
<td>$Firstname</td>n
<td>$Lastname</td>n
<tr>
<td>$Email</td>n
<tr>
<td>$Phone</td>n
</tr>n";
echo "<tr><td colspan='3'></td></tr>n";
}
echo "</table>n";

Thank you so much in advance for ANY assistance!
Veselin
2008-05-12 14:03:21
Re:
please take a look at INNER JOIN queiries used to extract data from two different tables based on a field
Reply
Title:
Your name:
Your email: (email address will not be posted on the web site)
Reply:
Verification