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>’;
}
}
?>

(Step 1: Wrap your code in [ PHP ] [/ PHP ] , no spaces)


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

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.


$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>';
}

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.

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’];

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.

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

// 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.