Jump to content


Pagation Problem


8 replies to this topic

#1 Ph0enix

    Young Padawan

  • Members
  • Pip
  • 33 posts

Posted 05 July 2006 - 08:44 PM

Hi i have this code that display 20 results from my database and then gives a link to another page that shows another 20 results but it doesnt work.. I get two errors these are:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/max/public_html/slimphoenix/mysite.php on line 84

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/max/public_html/slimphoenix/mysite.php on line 96

and here is the code.

<?php
define('DB_HOSTNAME','localhost');
define('DB_USERNAME','my_username');
define('DB_PASSWORD','my_password');
define('DB_NAME','my_dbname');

define('TABLE_NAME','Comments');
define('FIELD_NAME','user');
define('FIELD_TITLE','title');
define('FIELD_LINK','link');
define('FIELD_DATE','date');
define('RESULTS_PER_PAGE',20);

$res_conn = mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
mysql_select_db(DB_NAME,$res_conn);

$str_sql = "SELECT * FROM ".TABLE_NAME;
$res_result = mysql_query($str_sql,$res_conn);
$int_total = mysql_num_rows($res_result);							  //Line 84
if(isset($_GET['start']))
	$int_start = $_GET['start'];
else
	$int_start = 0;
if($int_start < 0)
	$int_start = 0;
if($int_start > $int_total-20)
	$int_start = $int_total-20;
	
$str_sql = "SELECT ".FIELD_NAME.",".FIELD_TITLE.",".FIELD_LINK.",DATE_FORMAT(".FIELD_DATE.",'%M %D, %Y') FROM ".TABLE_NAME." LIMIT ".$int_start.",".RESULTS_PER_PAGE;
$res_result = mysql_query($str_sql,$res_conn);
while($arr_data = mysql_fetch_assoc($res_result))				  //Lne 96
{
	echo '<b>Name:</b>'.$arr_data[FIELD_NAME].'<br/ >';
	echo '<b>Title:</b>'.$arr_data[FIELD_TITLE].'<br/ >';
	echo '<b>Link:</b><a href="'.$arr_data[FIELD_LINK].'">'.$arr_data[FIELD_LINK].'</a><br/ >';
	echo '<b>Date submitted:</b>'.$arr_data[FIELD_DATE].'<br/ >';
	echo '<hr />';
}
echo get_pages($int_total);

function get_pages($int_total)
{
	$int_pages = ceil($int_total/RESULTS_PER_PAGE);
	for($i=1;$i<=$int_pages;$i++)
	{
		$return .= '<a href="'.$_SERVER['PHP_SELF'].'?start='.(($i*20)-20).'">Page '.$i.'</a> ';
	}
	return $return;
}
?>

Please help me.
Thanks
Max

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 06 July 2006 - 02:03 AM

View PostPh0enix, on Jul 5 2006, 08:44 PM, said:

Hi i have this code that display 20 results from my database and then gives a link to another page that shows another 20 results but it doesnt work.. I get two errors these are:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/max/public_html/slimphoenix/mysite.php on line 84

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/max/public_html/slimphoenix/mysite.php on line 96

and here is the code.

<?php
define('DB_HOSTNAME','localhost');
define('DB_USERNAME','my_username');
define('DB_PASSWORD','my_password');
define('DB_NAME','my_dbname');

define('TABLE_NAME','Comments');
define('FIELD_NAME','user');
define('FIELD_TITLE','title');
define('FIELD_LINK','link');
define('FIELD_DATE','date');
define('RESULTS_PER_PAGE',20);

$res_conn = mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
mysql_select_db(DB_NAME,$res_conn);

$str_sql = "SELECT * FROM ".TABLE_NAME;
$res_result = mysql_query($str_sql,$res_conn);
$int_total = mysql_num_rows($res_result);							  //Line 84
if(isset($_GET['start']))
	$int_start = $_GET['start'];
else
	$int_start = 0;
if($int_start < 0)
	$int_start = 0;
if($int_start > $int_total-20)
	$int_start = $int_total-20;
	
$str_sql = "SELECT ".FIELD_NAME.",".FIELD_TITLE.",".FIELD_LINK.",DATE_FORMAT(".FIELD_DATE.",'%M %D, %Y') FROM ".TABLE_NAME." LIMIT ".$int_start.",".RESULTS_PER_PAGE;
$res_result = mysql_query($str_sql,$res_conn);
while($arr_data = mysql_fetch_assoc($res_result))				  //Lne 96
{
	echo '<b>Name:</b>'.$arr_data[FIELD_NAME].'<br/ >';
	echo '<b>Title:</b>'.$arr_data[FIELD_TITLE].'<br/ >';
	echo '<b>Link:</b><a href="'.$arr_data[FIELD_LINK].'">'.$arr_data[FIELD_LINK].'</a><br/ >';
	echo '<b>Date submitted:</b>'.$arr_data[FIELD_DATE].'<br/ >';
	echo '<hr />';
}
echo get_pages($int_total);

function get_pages($int_total)
{
	$int_pages = ceil($int_total/RESULTS_PER_PAGE);
	for($i=1;$i<=$int_pages;$i++)
	{
		$return .= '<a href="'.$_SERVER['PHP_SELF'].'?start='.(($i*20)-20).'">Page '.$i.'</a> ';
	}
	return $return;
}
?>

Please help me.
Thanks
Max


Well I can tell you what your problem is, but I'm not quite sure how to fix it at the moment.
Here's your line of error.
$str_sql = "SELECT * FROM ".TABLE_NAME;
If the 'TABLE_NAME' is supposed to be part of the query, it can't be outside the quotes. I can't quite tell what it's supposed to represent though. Is it a variable? Is it the actual name of the table? Whatever the case be, put it inside the quotes and it should be fine. Also you'll need to encase it with the `` marks for it to recognize it as a table. :)

#3 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 06 July 2006 - 02:11 AM

ok the error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/max/public_html/slimphoenix/mysite.php on line 84

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/max/public_html/slimphoenix/mysite.php on line 96

is simply telling you no rows have been pulled from the db, so it is the query, i think on the query you forgot to put ." at the end of TABLE_NAME.

Matt

#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 06 July 2006 - 02:38 AM

No, if he added another set of quotes at the end of that line, it would try to include the rest of the script in the variable. He simply needs to place the TABLE_NAME directly into the string instead of using operators to add it, and I believe it should work fine. As long as he adds it like `TABLE_NAME`.
I've run into this sort of thing all week on my own scripts, lol.

#5 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 06 July 2006 - 03:15 AM

ok...um...he doesnt need to change that string its fine. Its just ending the string in a variable.

Please run this:

<?php
define('DB_HOSTNAME','localhost');
define('DB_USERNAME','my_username');
define('DB_PASSWORD','my_password');
define('DB_NAME','my_dbname');

define('TABLE_NAME','Comments');
define('FIELD_NAME','user');
define('FIELD_TITLE','title');
define('FIELD_LINK','link');
define('FIELD_DATE','date');
define('RESULTS_PER_PAGE',20);

$res_conn = mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
mysql_select_db(DB_NAME,$res_conn);

$str_sql = "SELECT * FROM ".TABLE_NAME;
$res_result = mysql_query($str_sql,$res_conn) or die( mysql_error() );
$int_total = mysql_num_rows($res_result);							  //Line 84
if(isset($_GET['start']))
	$int_start = $_GET['start'];
else
	$int_start = 0;
if($int_start < 0)
	$int_start = 0;
if($int_start > $int_total-20)
	$int_start = $int_total-20;
	
$str_sql = "SELECT ".FIELD_NAME.",".FIELD_TITLE.",".FIELD_LINK.",DATE_FORMAT(".FIELD_DATE.",'%M %D, %Y') FROM ".TABLE_NAME." LIMIT ".$int_start.",".RESULTS_PER_PAGE;
$res_result = mysql_query($str_sql,$res_conn);
while($arr_data = mysql_fetch_assoc($res_result))				  //Lne 96
{
	echo '<b>Name:</b>'.$arr_data[FIELD_NAME].'<br/ >';
	echo '<b>Title:</b>'.$arr_data[FIELD_TITLE].'<br/ >';
	echo '<b>Link:</b><a href="'.$arr_data[FIELD_LINK].'">'.$arr_data[FIELD_LINK].'</a><br/ >';
	echo '<b>Date submitted:</b>'.$arr_data[FIELD_DATE].'<br/ >';
	echo '<hr />';
}
echo get_pages($int_total);

function get_pages($int_total)
{
	$int_pages = ceil($int_total/RESULTS_PER_PAGE);
	for($i=1;$i<=$int_pages;$i++)
	{
		$return .= '<a href="'.$_SERVER['PHP_SELF'].'?start='.(($i*20)-20).'">Page '.$i.'</a> ';
	}
	return $return;
}
?>

and post back with the error.

Quote

If the 'TABLE_NAME' is supposed to be part of the query, it can't be outside the quotes. I can't quite tell what it's supposed to represent though. Is it a variable? Is it the actual name of the table? Whatever the case be, put it inside the quotes and it should be fine. Also you'll need to encase it with the `` marks for it to recognize it as a table.

TABLE_NAME has been defined as a variable by define() at the top. Nothing needs to be added...if you really want to you can however try...
$str_sql = "SELECT * FROM `".TABLE_NAME."`";

It doesnt need to be in the quotes however, the way he has done it is correct.

Edited by .Matt, 06 July 2006 - 03:19 AM.


#6 Ph0enix

    Young Padawan

  • Members
  • Pip
  • 33 posts

Posted 06 July 2006 - 06:35 AM

Ok now i dont get any errors, it says now. Page 1
and i click on it but nothing happens and it doesnt show any results form my database. and when i click on page 1 ?start=0 is added on to the end of the address bar. Anyone know what the problem is now? There is definatley things in mt database.

#7 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 06 July 2006 - 06:59 AM

replace:

$res_result = mysql_query($str_sql,$res_conn);

with

$res_result = mysql_query($str_sql,$res_conn) or die( mysql_error() );

And post the error.

Edited by .Matt, 06 July 2006 - 06:59 AM.


#8 Ph0enix

    Young Padawan

  • Members
  • Pip
  • 33 posts

Posted 06 July 2006 - 11:24 AM

There is no error, just exactly the same problem.

#9 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 06 July 2006 - 12:53 PM

Problem(s) fixed via MSN.

Final code for reference:

<?php
define('DB_HOSTNAME','localhost');
define('DB_USERNAME','my_username');
define('DB_PASSWORD','my_password');
define('DB_NAME','my_dbname');

define('TABLE_NAME','Comments');
define('FIELD_NAME','user');
define('FIELD_TITLE','title');
define('FIELD_LINK','link');
define('FIELD_DATE','date');
define('RESULTS_PER_PAGE', '20');

function get_pages($int_total)
{
	$int_pages = ceil($int_total/RESULTS_PER_PAGE);
	
	for($i=1; $i<=$int_pages; $i++)
	{																		
		$return .= '<a href="'.$_SERVER['PHP_SELF'].'?start='.(($i*RESULTS_PER_PAGE)-RESULTS_PER_PAGE).'">Page '.$i.'</a> ';
	}
	return $return;
}

$res_conn = mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
mysql_select_db(DB_NAME,$res_conn);



$str_sql = "SELECT * FROM ".TABLE_NAME;

$res_result = mysql_query($str_sql,$res_conn) or die( mysql_error() );

$int_total = mysql_num_rows($res_result);			   

$int_start = (!is_numeric($_GET['start'])) ? $_GET['start'] : 0;		 
$int_start = (isset($_GET['start'])) ? $_GET['start'] : 0;



	
$str_sql2 = "SELECT * FROM ".TABLE_NAME." LIMIT ".$int_start.",".RESULTS_PER_PAGE;

$res_result2 = mysql_query($str_sql2,$res_conn) or die( mysql_error() );
$count = mysql_num_rows($res_result2);


while($arr_data = mysql_fetch_array($res_result2))				 
{
	  if(substr($arr_data[FIELD_LINK], 0, 7) != "http://")
	  {
			$arr_data[FIELD_LINK] = "http://". $arr_data[FIELD_LINK];
	  }
	
	echo '<b>Name:</b>'.$arr_data[FIELD_NAME].'<br/ >';
	echo '<b>Title:</b>'.$arr_data[FIELD_TITLE].'<br/ >';
	echo '<b>Link:</b><a href="'.$arr_data[FIELD_LINK].'" target="_blank">'.$arr_data[FIELD_LINK].'</a><br/ >';
	echo '<b>Date submitted:</b>'.$arr_data[FIELD_DATE].'<br/ >';
	echo '<hr />';
}

echo get_pages($int_total);


?>

Edited by .Matt, 06 July 2006 - 01:45 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users