...

View Full Version : why this code is not giving out any result?



muzammil
05-16-2011, 05:53 PM
<?php
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');

if (!isset($_GET['month'])) {
die("Invalid month specified.");
}
else {
$month = (int)$_GET['month'];
}

$result = mysql_query("SELECT timestamp, id, title FROM php_blog WHERE FROM_UNIXTIME(timestamp, '%m') = '$month' ORDER BY id DESC");

while ($row = mysql_fetch_array($result)) {
$date = date("l F d Y", $row['timestamp']);
$id = $row['id'];
$title = stripslashes($row['title']);

?>

<p><?php echo $date; ?><br /><a href="journal.php?id=<?php echo $id; ?>"><?php echo $title; ?></a></p>
<?php
}

?>

Fou-Lu
05-16-2011, 06:00 PM
There is only two reasons:
The first is that your query has failed causing your script to err. Check the error logs for PHP, or add


ini_set('display_errors', 1);
error_reporting(E_ALL);

To the script to see. If its a failure on the mysql_query, you should be catching the error before trying to process it.

Second: If no error is caused, the data has no match for the criteria you have given it. The only heads up I can give on that is that %m should be giving you a 0 based month, so january will be 01, so you need to match that against that input from the user.

muzammil
05-16-2011, 06:28 PM
Sir I've myself entered the input. and there is data available in database. I have checked from 01 to 12, Nothing is coming up.
The same code gives the result if I use %Y....
I m confused :-( I m new coder practicing codes...You can check it on my website what I mean.

www.meerab.info/muzammil/mblog.php

In the archive section It is working if I group data by year, but not by month.

Fou-Lu
05-16-2011, 06:46 PM
I don't follow links from work.
That said, the problem is actually easier than I thought, can't believe I missed:


if (!isset($_GET['month'])) {
die("Invalid month specified.");
}
else {
$month = (int)$_GET['month'];
}

An integer can never be 0 based. That needs to be reconverted to a string, but I'd still cast:


if (!isset($_GET['month'])) {
die("Invalid month specified.");
}
else {
$month = str_pad((int)$_GET['month'], 2, 0, STR_PAD_LEFT);
}

Try that.

mic2100
05-16-2011, 06:53 PM
lol Fou i was just about to write the same thing :P

muzammil
05-16-2011, 06:54 PM
@FOU You're awsome!!! Thanks for bearing with me!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum