SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Please Can Anyone Spot what is wrong with this code

    I have tried to use this code but I can get it to work well in my server. When I run the mysql query in mysql it gives me the result that I want but within the code it does not output anything. It just gives a blank page. What can be wrong please.

    <?php
    if(isset($_GET['page2']))
    {
    include 'dbconnect.inc';
    $year = $_GET['page2'];
    $query = "select distinct MONTH(publicationdate) from agricultural_research_journal where year(publicationdate) = '$year' order by id asc";
    $result = mysql_query($query) or die('Error, query failed');
    $num_result = mysql_num_rows($result);

    //to pick the dates
    for ($i=0; $i <$num_result; $i++)
    {
    $story = mysql_fetch_assoc($result);
    print '<table width = "385" border="1" align = "center">';
    print '<tr>';
    print '<td>';echo '$num_result';
    $my_date = mysql_result($result, $i);
    echo "<a href=month.php?month=$my_date>$my_date</a>";
    print '</td>';
    print '</tr>';
    print '</table>';
    }
    }
    ?>

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    (Step 1: Wrap your code in [ PHP ] [/ PHP ] , no spaces)

    PHP Code:
    <?php
    if(isset($_GET['page2']))
    {
    include 
    'dbconnect.inc';
    $year $_GET['page2'];
    $query "select distinct MONTH(publicationdate) from agricultural_research_journal where year(publicationdate) = '$year' order by id asc";
    $result mysql_query($query) or die('Error, query failed');
    $num_result mysql_num_rows($result);

    //to pick the dates
    for ($i=0$i <$num_result$i++)
    {
    $story mysql_fetch_assoc($result);
    print 
    '<table width = "385" border="1" align = "center">';
    print 
    '<tr>';
    print 
    '<td>';echo '$num_result';
    $my_date mysql_result($result$i);
    echo 
    "<a href=month.php?month=$my_date>$my_date</a>";
    print 
    '</td>';
    print 
    '</tr>';
    print 
    '</table>';
    }
    }
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Step 2: Ignoring for the moment that your query is a security risk waiting to happen (always clean up user input. If you're expecting an integer, do an is_int check), I have trouble following the logic for retrieving your results. Let's try simplifying that first.
    PHP Code:
    $query "select distinct MONTH(publicationdate) AS themonth FROM agricultural_research_journal WHERE YEAR(publicationdate) = '$year' order by id asc";
    $result mysql_query($query) or die('Error, query failed');

    while(
    $story mysql_fetch_assoc($result)) {
     print 
    '<table width = "385" border="1" align = "center">';
    print 
    '<tr>';
    print 
    '<td>';echo '$num_result';
    echo 
    "<a href=month.php?month=".$story['themonth'].">".$story['themonth']."</a>";
    print 
    '</td>';
    print 
    '</tr>';
    print 
    '</table>';

    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    mysql_result() returns the value from the cell you specified so it should be like this:

    $my_date = mysql_result($result, 0);

    echo '$num_result'; should be echo "$num_result";

    EDIT: You should get some output if the query is correct. Do what StarLion and DaveMaxell suggested and try again.

  5. #5
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,267
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    Did you do any of the basic debug steps?

    1) echo out the query?
    2) echo out the num_results
    3) var dump to results object

    On first glance, this looks off: $year = $_GET['page2'];
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  6. #6
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I have checked the codes again. I got the codes wrong from the variable aspect. I called $_Get ['page2'] instead of calling for $_Get['year'] which is what I expected from the page I am getting the variable from. Thanks for your suggestions.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Learn from this, and like DaveMaxwell intimates get used to using some debug as you work along.

    If you have a script which is going to be dependent upon incoming variables such as GET or POST then get used to echoing them out onto the page at the top of your script.

    PHP Code:
    <?php

    // comment this out or remove it when you have affirmed that the incoming
    // vars actually meet your expectation

    var_dump($_GET);
    echo 
    '<hr />';
    Do the same for $_POST too ..

    Get into the swing of having PHP prove to you that what you think should be happening matches what PHP is doing.


Bookmarks

Posting Permissions

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