The Mysterious Case of the Unwilling Variables??
Date: 11/04/05
(PHP Community) Keywords: php, mysql, database, sql
Hi, I'm completely stumped over this. I'm probably missing something quite obvious, and I figure it's to do with using global variables.
There is a file, PostDisplay.php. It loops through an array of posts from the database and prints them out. It is independent of the posts themselves and is called by scripts that specify the posts to be printed. Before the loop are the queries to put the posts into an array and count the number of rows (used in the loop).
$array_post = mysql_query($query_post);
$num_post = mysql_num_rows($array_post);
There are two files so far to specify what posts to print. This one, LatestPost.php, works perfectly.
$latest_post = "SELECT * FROM posts ORDER BY id DESC LIMIT 1";
$query_post = $latest_post;
include('PostDisplay.php');
?>
This one, MonthsPost.php, doesn't.
$year = $_GET['year']; # sets the year of the desired posts as a global variable
$month = $_GET['month']; # sets the month of the desired posts as a global variable
$month_post = "SELECT * FROM posts WHERE month=$month && year=$year";
$query_post = $month_post;
include('PostDisplay.php');
?>
The global variables $year and $month are set in the script months.inc.php. Excerpt:
November
The error given upon loading MonthsPost.php is:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\phpdev5\www\new\PostDisplay.php on line 4
(Line 4 is "$num_post = mysql_num_rows($array_post);".)
If anyone is brave/bored enough to read all that and help, thank you. ^_^
Edit: Using mysql_select_db in PostDisplay.php has fixed it, and so I've done something silly with the includes elsewhere. Thanks for all the help ^_^
Source: http://www.livejournal.com/community/php/364431.html