/    Sign up×
Community /Pin to ProfileBookmark

Search database – how to pull data from child tables? (beginner, sorry)

Hello,

I’m a beginner with databases and MySQL/PHP, but know enough to macguyver things together …somewhat.

I’ve managed to create a database with three tables – Merchants (a list businesses and their contact details), Issues (issues with merchants), Updates (updates to issues with merchants).

I’ve set my foreign keys up as follows:

Merchants – no FK
Issues – MerchantID to Merchants > MerchantID
Updates – IssueID to Issues > IssueID

I’m using this mini tutorial to set up my first database search page: [url]http://teamtutorials.com/web-development-tutorials/php-tutorials/creating-a-form-that-will-search-a-mysql-database#.UKVJhobZ6YF[/url]

I’m trying to set up a single search box where if a user types in a merchant’s name, it brings up that merchant plus all it’s issues and updates against those issues.

I’ve set out my code below in a logical fashion but I don’t know the correct syntax/operators/functions to accomplish this, let alone if I need to append the three sql statements as $sql1 = mysql_query, $sql2 = mysql_query, $sql3 = mysql_query

Please would you be able to advise me on any handy pointers to enable me to accomplish this?

The code:

[code]<form action=”search.php” method=”post”>
Search: <input type=”text” name=”term” />
<br />
<input type=”submit” name=”submit” value=”Submit” />
</form>

<p></p>

<?php
mysql_connect (“localhost”, “username”,”password”) or die (mysql_error());
mysql_select_db (“dbname”);

$term = $_POST[‘term’];

$sql = mysql_query(“select * from Merchants where Name like ‘%$term%'”);

while ($row = mysql_fetch_array($sql)){
echo ‘Merchant #’.$row[‘MerchantID’];
echo ‘<br/> Name: ‘.$row[‘Name’];
echo ‘<br/> Phone: ‘.$row[‘Phone’];
echo ‘<br/> Email: ‘.$row[‘Email’];
echo ‘<br/>’.$row[‘Private?’];
echo ‘<br /><br />’;
}

$sql = mysql_query(“select * from Issues if MerchantID matches Merchants > MerchantID”);

while ($row = mysql_fetch_array($sql)){
echo ‘Issue #’.$row[‘IssueID’];
echo ‘<br/> Date of issue: ‘.$row[‘IssueDate’];
echo ‘<br/> Issue: ‘.$row[‘Issue’];
echo ‘<br/> Solution: ‘.$row[‘Solution’];
echo ‘<br /><br />’;
}

$sql = mysql_query(“select * from Updates if IssueID matches Issues > IssueID”);

while ($row = mysql_fetch_array($sql)){
echo ‘Update #’.$row[‘UpdateID’];
echo ‘<br/> Date of update: ‘.$row[‘UpdateDate’];
echo ‘<br/> Update: ‘.$row[‘Update’];
echo ‘<br /><br />’;
}

?>[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@simplypixieNov 16.2012 — If you are more than one query on a page, then yes you need to name the variables that the queries are assigned to differently ($sql1, $sql2 etc or whatever you want to call the variables). You also need to change your $row variable name for each while loop to prevent any conflict (i.e. $row2, $row3).

With regard to your actual queries, they should be:
[code=php]$sql2 = mysql_query("select * from Issues where MerchantID='".$row['MerchantID']."'");
while ($row2 = mysql_fetch_array($sql2) {
....
// and
$sql3 = mysql_query("select * from Updates where IssueID='".$row2['IssueID']."'");
while ($row3 = mysql_fetch_array($sql3) {
....
[/code]
Copy linkTweet thisAlerts:
@SC7639Nov 16.2012 — You could do this with just two queries using "JOIN" in your query.

PHP Code:
[code=php]$sql = mysql("select * from Merchants join Issues using(MerchantID) where Name like '%$term%'");
while($row = mysql_fetch_array($sql))
{
echo 'Merchant #'.$row['MerchantID'];
echo '<br/> Name: '.$row['Name'];
echo '<br/> Phone: '.$row['Phone'];
echo '<br/> Email: '.$row['Email'];
echo '<br/>'.$row['Private?'];
echo '<br /><br />';

echo 'Issue #'.$row['IssueID'];
echo '<br/> Date of issue: '.$row['IssueDate'];
echo '<br/> Issue: '.$row['Issue'];
echo '<br/> Solution: '.$row['Solution'];
echo '<br /><br />';
}

$sql2 = mysql_query("select * from Update where IssueID = '" . $row['IssueID'] . "'");
while($row2 = mysql_fetch_array($sql2))
{
echo 'Update #'.$row['UpdateID'];
echo '<br/> Date of update: '.$row['UpdateDate'];
echo '<br/> Update: '.$row['Update'];
echo '<br /><br />';
}[/code]
Copy linkTweet thisAlerts:
@W8_4meauthorNov 17.2012 — Thank you very much everyone, your help is really appreciated.

If I may ask another question, I've set up a draft of the markup that should be generated by the queries, below (using SC7639's quries).

I was wondering that if I use tables, this structure would seem to require that the Phone and Email results are echoed in the second query - but the data Phone/Email does not exist in the Updates table (see under <!-- 2nd end of merchant/issue query results -->)

Thinking about it, I guess I could query as normal using div's and CSS to position, thereby working around this problem. However I was hoping that it'd be more straight forward in PHP.

Also, is there a way to use something like the EOF thing in a PHP mailer to output a chunk of HTML rather than outputting line by line with echo? It's going to be a bit laboursome to echo this table!

&lt;!-- begin merchant/issue query results --&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td width=65&gt;&lt;b&gt;Name:&lt;/b&gt;&lt;/td&gt;
&lt;td width=225&gt;Name&lt;/td&gt;
&lt;td rowspan=3&gt;

&lt;!-- begin issues --&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td colspan=2&gt;&lt;b&gt;Issue ID:&lt;/b&gt; IssueID / &lt;b&gt;Date:&lt;/b&gt; IssueDate / &lt;b&gt;Deal ID:&lt;/b&gt; DealID / &lt;b&gt;Deal Date &lt;i&gt;(valid from)&lt;/i&gt;:&lt;/b&gt; DealDate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=valigntop&gt;&lt;b&gt;Deal Title:&lt;/b&gt; DealTitle&lt;/td&gt;
&lt;td width=165 class=valigntop&gt;&lt;b&gt;Category:&lt;/b&gt; Category&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan=2 class=valigntop&gt;
&lt;b&gt;Issue:&lt;/b&gt;
&lt;p&gt;&lt;/p&gt;
Issue
&lt;p&gt;&lt;/p&gt;
&lt;b&gt;Solution:&lt;/b&gt;
&lt;p&gt;&lt;/p&gt;
Solution
&lt;p&gt;
&lt;b&gt;Updates:&lt;/b&gt;
&lt;/p&gt;
&lt;!-- 1st end of merchant/issue query results --&gt;

&lt;!-- begin update query results --&gt;
&lt;span class=updates&gt;
&lt;b&gt;UpdateDate&lt;/b&gt;
&lt;p&gt;&lt;/p&gt;
Update
&lt;/span&gt;
&lt;p&gt;&lt;/p&gt;

&lt;!-- end issues --&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;/p&gt;

&lt;!-- 2nd end of merchant/issue query results --&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Phone:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Phone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Email:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;i&gt;Private?&lt;/i&gt; Email&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
×

Success!

Help @W8_4me spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...