Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2010
    Location
    England
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP link to MySQL to show database records

    Hi all,

    I am looking to write some PHP that looks at the user's input from a search box, and then displays the results accordingly on a new page (data taken from my MySQL table). I have succeeded in doing this so far.

    What I am now looking to do is, when the results are displayed, I want the user to be able to click on one of the results and then for a new page to display information from my database regarding this particular record.

    This is the code I have used so far for connect to the database and display the results depending on the search criteria.

    PHP Code:
    <?php
        $con 
    mysql_connect(" ******* "," ****** "," ****** ");
            if (!
    $con)
                {
                die(
    'Could not connect to database');
                }
                            
            
    mysql_select_db(" ****** "$con);
                            
            
    $keywords $_POST['key_words'];
                            
            
    $sql "SELECT Surname, First_Name
                   FROM Squad_List
                   WHERE Surname LIKE '%$keywords%'
                   OR First_Name LIKE '%$keywords%'
                   ORDER BY Surname"
    ;
                            
            
    $rst mysql_query($sql$con);
                            
            while(
    $a_row mysql_fetch_assoc($rst)) {
                 echo (
    "<a href='player.php'>{$a_row['First_Name']} {$a_row['Surname']}</a><br>" );
            }
                            
            
    mysql_close($con); //close the connection to the database
       
    ?>
    How would I link each individual record to a new page displaying info about that particular link with information from my MySQL records?

  • #2
    New Coder
    Join Date
    Oct 2010
    Location
    England
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post No worries

    I've sorted it - well, it works.

    Code I've used for displaying and linking to new page with info:

    PHP Code:

        <?php
            $con 
    mysql_connect("*****","*****","*****");
            if (!
    $con)
              {
              die(
    'Could not connect to database');
              }
            
            
    mysql_select_db("*****"$con);
            
            
    $keywords $_POST['key_words'];
            
            
    $sql "SELECT Player_id, Surname, First_Name
                    FROM Squad_List
                    WHERE Surname LIKE '%$keywords%'
                    OR First_Name LIKE '%$keywords%'
                    ORDER BY Surname"
    ;
            
            
    $rst mysql_query($sql$con);
            
            while(
    $a_row mysql_fetch_assoc($rst)) {
                print (
    "<a href='player.php?id={$a_row['Player_id']}'>{$a_row['First_Name']} {$a_row['Surname']}</a><br>" );
            }
            
            
    mysql_close($con); //close the connection to the database
        
    ?>

    Code I've used on the new page:


    PHP Code:

        <?php
            $con 
    mysql_connect("*****","*****","*****");
            if (!
    $con)
              {
              die(
    'Could not connect to database');
              }
            
            
    mysql_select_db("*****"$con);
            
            
    $id $_GET['id'];
            
            
    $sql "SELECT * FROM Squad_List WHERE Player_id = {$id}";
            
            
    $rst mysql_query($sql$con);
            
    $a_row mysql_fetch_assoc($rst);
            
            print (
    "{$a_row['First_Name']} {$a_row['Surname']}</a><br>" );
            print (
    "{$a_row['Position']}<br>" );
            print (
    "{$a_row['Age']}<br>" );
            
            
        
    ?>


  •  

    Tags for this Thread

    Posting Permissions

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