Linking multiple MySQL database results to their own page through PHP
Results 1 to 9 of 9

Thread: Linking multiple MySQL database results to their own page through PHP

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    5

    Linking multiple MySQL database results to their own page through PHP

    Hi there, Iím hoping someone can help me, Iíve very new to PHP (as you can probably tell by the poorly worded title) so please bear with me and I hope this makes some sense!

    I have a webpage that displays articles from a MySQL database, each article is made up of a unique ID, Date, Title, Summary and Text. I have a page which displays all articles from within the database (code shown below)

    <?php
    $db = mysql_connect("localhost", "blabla", "password");
    mysql_select_db ("blabla", $db);
    $result = mysql_query("SELECT * FROM Articles");//
    while($row=mysql_fetch_array($result))
    {
    echo$row['ID'];//
    echo "</br>";
    echo$row['Date'];//
    echo "</br>";
    echo$row['Title'];//
    echo "</br>";
    echo$row['Summary'];//
    echo "</br>";
    echo$row['Text'];//
    }
    ?>

    This page works fine but I want to be able to make an option within the page so that each article can be linked to its own page (displaying the articles ID, date, title, summary and text), so that if a button or the article ID is to be clicked upon that selected article will open in its own separate page. I am having major trouble doing this, I tried to send it through a URL but that was a pretty stupid idea for that amount of data from a database and didnít work. I was wondering how I can go about making it so every article can be viewed separately in a page of its own.
    Thank you so much!

  2. #2
    Syntax Error
    Join Date
    Feb 2003
    Location
    San Diego, CA
    Posts
    443
    It sounds like what you need to do is send only the ID through the URL. For example:

    article.php?article_id=1

    Then article.php will look for the ID (e.g. $_GET['article_id']) and query the database WHERE `ID` = 1

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Quote Originally Posted by m@tt View Post
    It sounds like what you need to do is send only the ID through the URL. For example:

    article.php?article_id=1

    Then article.php will look for the ID (e.g. $_GET['article_id']) and query the database WHERE `ID` = 1
    Hi there, i've done this but it only returns the ID for this article. I want the corresponding fields which go with this article's ID; title, data, summary and text to show itself as well.

  4. #4
    Syntax Error
    Join Date
    Feb 2003
    Location
    San Diego, CA
    Posts
    443
    I'm not sure if you understood what I meant. Basically on your article.php you would run the query again to fetch the ID from the URL. e.g.

    PHP Code:
    // Define ID from URL
    $id = (int) $_GET['article_id']; // It is good practice to typecast input integers

    // Query
    $result mysql_query("SELECT * FROM Articles WHERE `ID` = $id"); 
    Does that make sense?

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Quote Originally Posted by m@tt View Post
    I'm not sure if you understood what I meant. Basically on your article.php you would run the query again to fetch the ID from the URL. e.g.

    PHP Code:
    // Define ID from URL
    $id = (int) $_GET['article_id']; // It is good practice to typecast input integers

    // Query
    $result mysql_query("SELECT * FROM Articles WHERE `ID` = $id"); 
    Does that make sense?
    Ohhhhhh o.k. thank you! Yeah it makes sense, i've got it sending the ID through the URL and then have that code on the view article page, do I have to echo any variables to display the result?

  6. #6
    Syntax Error
    Join Date
    Feb 2003
    Location
    San Diego, CA
    Posts
    443
    You would fetch the results and echo the same as you did before. The only difference is you now only have one result

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Quote Originally Posted by m@tt View Post
    You would fetch the results and echo the same as you did before. The only difference is you now only have one result
    The article listing page now has this added
    $ID = $row["ID"];
    echo "<a href=ViewArticle.php?ID=$ID>View Article</a>";
    echo "</br>";
    $Date = $row["Date"];
    echo$row['Date'];//

    instead of just;
    echo$row['ID'];//
    echo "</br>";
    echo$row['Date'];//

    When I click on an article ID it sends that ID number across to the view article page in the URL but this is my code now for the view article page, its not showing any result just a blank page


    <?php
    $db = mysql_connect("localhost", "blablabla", "password");
    mysql_select_db ("blabla", $db);

    $ID =($_GET['ID']);

    $result = mysql_query("SELECT * FROM Articles WHERE ID = $ID");//

    while(mysql_fetch_array($result))
    {
    echo$row['ID'];//
    echo "</br>";
    echo$row['Date'];//
    echo "</br>";
    echo$row['Title'];//
    echo "</br>";
    echo$row['Summary'];//
    echo "</br>";
    echo$row['Text'];//
    echo "</br>";
    }
    ?>


    Sorry im really rubbish with this haha

  8. #8
    Syntax Error
    Join Date
    Feb 2003
    Location
    San Diego, CA
    Posts
    443
    First you should always typecast integers that are input values (GET/POST) so you would want to do

    PHP Code:
    $ID = (int) $_GET['ID']; 
    The query looks fine but you are not fetching the results properly since mysql_fetch_array is not being assigned to $row.

    PHP Code:
    while($row mysql_fetch_array($result)) 
    Since you are only expecting one result the while loop is unnecessary. You should remove that but keep the $row = mysql_fetch_array($result)

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Quote Originally Posted by m@tt View Post
    First you should always typecast integers that are input values (GET/POST) so you would want to do

    PHP Code:
    $ID = (int) $_GET['ID']; 
    The query looks fine but you are not fetching the results properly since mysql_fetch_array is not being assigned to $row.

    PHP Code:
    while($row mysql_fetch_array($result)) 
    Since you are only expecting one result the while loop is unnecessary. You should remove that but keep the $row = mysql_fetch_array($result)
    Matt you are actually amazing! It's all working now thank you so so so much

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
  •