www.webdeveloper.com
Results 1 to 5 of 5

Thread: Search database - how to pull data from child tables? (beginner, sorry)

  1. #1
    Join Date
    Jan 2003
    Location
    UK
    Posts
    279

    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: http://teamtutorials.com/web-develop...e#.UKVJhobZ6YF

    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 />';
        }
    	
    ?>

  2. #2
    Join Date
    Oct 2012
    Posts
    39

  3. #3
    Join Date
    Aug 2012
    Posts
    155
    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:
    PHP Code:
    $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) {
    .... 

  4. #4
    Join Date
    Nov 2012
    Posts
    2
    You could do this with just two queries using "JOIN" in your query.

    PHP Code:
    PHP Code:
    $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 />';


  5. #5
    Join Date
    Jan 2003
    Location
    UK
    Posts
    279
    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!

    Code:
    <!-- begin merchant/issue query results -->
    <table>
    <tr>
    <td width=65><b>Name:</b></td>
    <td width=225>Name</td>
    <td rowspan=3>
    
    <!-- begin issues -->
    <table>
    <tr>
    <td colspan=2><b>Issue ID:</b> IssueID / <b>Date:</b> IssueDate / <b>Deal ID:</b> DealID / <b>Deal Date <i>(valid from)</i>:</b> DealDate</td>
    </tr>
    <tr>
    <td class=valigntop><b>Deal Title:</b> DealTitle</td>
    <td width=165 class=valigntop><b>Category:</b> Category</td>
    </tr>
    <tr>
    <td colspan=2 class=valigntop>
    <b>Issue:</b>
    <p></p>
    Issue
    <p></p>
    <b>Solution:</b>
    <p></p>
    Solution
    <p>
    <b>Updates:</b>
    </p>
    <!-- 1st end of merchant/issue query results -->
    
    <!-- begin update query results -->
    <span class=updates>
    <b>UpdateDate</b>
    <p></p>
    Update
    </span>
    <p></p>
    
    <!-- end issues -->
    </td>
    </tr>
    </table>
    <p></p>
    
    <!-- 2nd end of merchant/issue query results -->
    </td>
    </tr>
    <tr>
    <td><b>Phone:</b></td>
    <td>Phone</td>
    </tr>
    <tr>
    <td><b>Email:</b></td>
    <td><i>Private?</i> Email</td>
    </tr>
    </table>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10339 seconds
  • Memory Usage 2,902KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates