Hello, I am trying to select data from two tables and using mysql_fetch_array to display them. I have two tables called person and membership. person has 9 fields like personID (primary key), first, last, address etc. and the 2nd table has 5 fields like personID (foreign key), membershipID (primary key), commenced, renewed, type.
What I want to do is search through all the fields on both tables and then print them out in a table. My code worked fine when I was only searching one table yet when I try and search through them both I get many repeated results. I managed to find a similar thread but I still could not get it to work with my script. http://www.phpbuilder.com/board/showthread.php?t=618734
$query = "select * from person,membership WHERE person.personID LIKE '$srch' || person.first LIKE '$srch' || person.last LIKE '$srch' || person.address LIKE '$srch' || person.town LIKE '$srch' || person.state LIKE '$srch' || person.post LIKE '$srch' || person.email LIKE '$srch' || person.phone LIKE '$srch'|| membership.personID LIKE '$srch' || membership.membershipID LIKE '$srch' || membership.commenced LIKE '$srch' || membership.renewed LIKE '$srch' || membership.type LIKE '$srch'";
$result=mysql_query($query);
if ($result)
{
echo "Here are the results:<br><br>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF>PersonID</td>
<td align=center bgcolor=#00FFFF>First</td>
<td align=center bgcolor=#00FFFF>Last</td>
<td align=center bgcolor=#00FFFF>Address</td>
<td align=center bgcolor=#00FFFF>Town</td>
<td align=center bgcolor=#00FFFF>State</td>
<td align=center bgcolor=#00FFFF>Post</td>
<td align=center bgcolor=#00FFFF>Email</td>
<td align=center bgcolor=#00FFFF>Phone</td>
<td align=center bgcolor=#00FFFF>Membership ID</td>
<td align=center bgcolor=#00FFFF>Commenced</td>
<td align=center bgcolor=#00FFFF>Renewed</td>
<td align=center bgcolor=#00FFFF>type</td>
</tr>";
while ($r = mysql_fetch_array($result)) {
$personID = $r["personID"];
$first = $r["first"];
$last = $r["last"];
$address = $r["address"];
$town = $r["town"];
$state = $r["state"];
$post = $r["post"];
$email = $r["email"];
$phone = $r["phone"];
$membershipID = $r["membershipID"];
$commenced = $r["commenced"];
$renewed = $r["renewed"];
$type = $r["type"];
echo "<tr>
<td>$personID</td>
<td>$first</td>
<td>$last</td>
<td>$address</td>
<td>$town</td>
<td>$state</td>
<td>$post</td>
<td>$email</td>
<td>$phone</td>
<td>$membershipID</td>
<td>$commenced</td>
<td>$renewed</td>
<td>$type</td>
</tr>";
}
echo "</table>";
} else { echo "problems...."; }
} else {
echo "Search string is empty. <br> Go back and type a string to search";
}
My two tables are as follows-
CREATE TABLE person (
personID int(6) NOT NULL auto_increment,
first varchar(15) NOT NULL,
last varchar(15) NOT NULL,
address varchar(30) NOT NULL,
town varchar(15) NOT NULL,
state varchar(3) NOT NULL,
post int(4) NOT NULL,
email varchar(30) NOT NULL,
phone varchar(15) NOT NULL,
PRIMARY KEY (personID),
UNIQUE KEY id (personID),
KEY id_2 (personID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE membership (
personID int(6) NOT NULL,
membershipID int(6) NOT NULL auto_increment,
commenced date NOT NULL,
renewed date default NULL,
type varchar(16) NOT NULL,
PRIMARY KEY (membershipID),
KEY personID (personID),
CONSTRAINT membership_ibfk_1 FOREIGN KEY (personID) REFERENCES person (personID) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;