Page 1 of 1

sql error

Posted: Sun Apr 23, 2006 1:32 pm
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'" );


?>

Posted: Sun Apr 23, 2006 1:42 pm
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?

Posted: Sun Apr 23, 2006 2:17 pm
by psychotomus
supplied argument is not a valid MySQL result resource

which means. it work for all others..

Posted: Sun Apr 23, 2006 2:49 pm
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);

Posted: Sun Apr 23, 2006 3:49 pm
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

Posted: Sun Apr 23, 2006 3:56 pm
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.

Posted: Mon Apr 24, 2006 2:36 am
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.

Posted: Tue Apr 25, 2006 12:13 pm
by psychotomus
i fixed the problem.