sql error

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

sql error

Post by psychotomus »

error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mounted-storage/home16a/sub002/sc18478-RGIJ/www.po2upload.com/update_stats.php on line 45

error on line 45

the rest of the code works up till line 45

Code: Select all

//Stats By Month
$result = mysql_query( "SELECT * FROM po2StatsByMonth WHERE month='$themonth' AND year='$theyear'" );//
$row=mysql_fetch_array($result);	//Store Record Of Data in $row

Code: Select all

<?php
require_once 'config.php';
/////Visitors a month. Page views per month
////////////
///////////
/////////
////////////
///////////
$hostname  = $_SERVER['REMOTE_ADDR'];

//Get Stats
$result = mysql_query( "SELECT * FROM po2_stats WHERE id='1'" );//
$row=mysql_fetch_array($result);	//Store Record Of Data in $row

//increase page views
$PageViewsStats = $row['PageViews'] + 1;


//check if visitor allready visited.
$result = mysql_query( "SELECT * FROM po2_visitors WHERE ip='$hostname'" );//
$row=mysql_fetch_array($result);	//Store Record Of Data in $row 


//if ip doesnt exist
if ($row['id'] == "")
{
	//increase Visitor Count
	$VisitorStats = $VisitorStats + 1;
	//add visitor IP to visitor Table
	mysql_query ("INSERT INTO po2_visitors(ip) VALUES ('$hostname')") 
			or die(mysql_error());
			

}

//reset PageViews in Stats Table
mysql_query ("UPDATE po2_stats SET PageViews='$PageViewsStats' WHERE id='1'") or die(mysql_error());

$themonth = date("m");
$theyear = date("Y");
$theday = date("d");

//Stats By Month
$result = mysql_query( "SELECT * FROM po2StatsByMonth WHERE month='$themonth' AND year='$theyear'" );//
$row=mysql_fetch_array($result);	//Store Record Of Data in $row 


if ($row['id'] == "")
{
	mysql_query ("INSERT INTO po2_StatsByMonth(Visitors, PageViews, Downloads, month, year) VALUES ('0', '0', '0', '$themonth', '$theyear')") 
			or die(mysql_error());
	
	mysql_query ("INSERT INTO po2_VisitorsByMonth(ip, month, year) VALUES ('$hostname', '$themonth', '$theyear')") 
			or die(mysql_error());
	
	$VisitorsByMonth = 1;
	$PageViewsByMonth = 1;
}
else
{
	$VisitorsByMonth = $row['Visitors'];
	$PageViewsByMonth = $row['PageViews'] + 1;

	$result = mysql_query( "SELECT * FROM po2_VisitorsByMonth WHERE ip='$hostname' AND month='$themonth' AND year='$theyear'" );//
	$row=mysql_fetch_array($result);	//Store Record Of Data in $row 

	if ($row['id'] == "")
	{
		mysql_query ("INSERT INTO po2_VisitorsByMonth(ip, month, year) VALUES ('$hostname', '$themonth', '$theyear')") 
			or die(mysql_error());

		$VisitorsByMonth = $VisitorsByMonth + 1;
	}
}

mysql_query ("UPDATE po2_StatsByMonth SET Visitors='$VisitorsByMonth', PageViews='$PageViewsByMonth' WHERE month='$themonth' AND year='$theyear'" );


/////////////////////
////////////////
////////Stats by day, visitors and page views.

//Stats By dat
$result = mysql_query( "SELECT * FROM po2_StatsByDay WHERE month='$themonth' AND year='$theyear' AND day='$theday'" );//
$row=mysql_fetch_array($result);	//Store Record Of Data in $row 

if ($row['id'] == "")
{
	mysql_query ("INSERT INTO po2_StatsByDay(Visitors, PageViews, month, year, day) VALUES ('0', '0', '$themonth', '$theyear','$theday')") 
			or die(mysql_error());
	
	mysql_query ("INSERT INTO po2_VisitorsByDay(ip, month, year, day) VALUES ('$hostname', '$themonth', '$theyear','$theday')") 
			or die(mysql_error());
	
	$VisitorsByDay = 1;
	$PageViewsByDay = 1;
}
else
{
	$VisitorsByDay = $row['Visitors'];
	$PageViewsByDay = $row['PageViews'] + 1;

	$result = mysql_query( "SELECT * FROM po2_VisitorsByDay WHERE ip='$hostname' AND month='$themonth' AND year='$theyear' AND day='$theday'" );//
	$row=mysql_fetch_array($result);	//Store Record Of Data in $row 

	if ($row['id'] == "")
	{
		mysql_query ("INSERT INTO po2_VisitorsByDay(ip, month, year, day) VALUES ('$hostname', '$themonth', '$theyear', '$theday')") 
			or die(mysql_error());

		$VisitorsByDay = $VisitorsByDay + 1;
	}
}

mysql_query ("UPDATE po2_StatsByDay SET Visitors='$VisitorsByDay', PageViews='$PageViewsByDay' WHERE month='$themonth' AND year='$theyear' AND day='$theday'" );


?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$result = mysql_query( "SELECT * FROM po2StatsByMonth WHERE month='$themonth' AND year='$theyear'" ) or die(mysql_error());

add the bolded text, and what does it tell you?
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

supplied argument is not a valid MySQL result resource

which means. it work for all others..
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

Jcart wrote:$result = mysql_query( "SELECT * FROM po2StatsByMonth WHERE month='$themonth' AND year='$theyear'" ) or die(mysql_error());

add the bolded text, and what does it tell you?

the error is on the next line. not that line.

Code: Select all

$row=mysql_fetch_array($result);
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If you want to reduce a couple of queries:

Code: Select all

INSERT INTO table (col1, col2) VALUES (val1, val2)
ON DUPLICATE KEY UPDATE pageviews = pageview + 1
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

timvw wrote:If you want to reduce a couple of queries:

Code: Select all

INSERT INTO table (col1, col2) VALUES (val1, val2)
ON DUPLICATE KEY UPDATE pageviews = pageview + 1
thanks will use that later... that still doesnt fix my current problem.
tonera
Forum Newbie
Posts: 5
Joined: Mon Apr 10, 2006 10:40 pm

Post by tonera »

Jcart wrote:$result = mysql_query( "SELECT * FROM po2StatsByMonth WHERE month='$themonth' AND year='$theyear'" ) or die(mysql_error());

add the bolded text, and what does it tell you?
or insert a line:

echo "SELECT * FROM po2StatsByMonth WHERE month='$themonth' AND year='$theyear'";

before it and tell us the result.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

i fixed the problem.
Post Reply