/    Sign up×
Community /Pin to ProfileBookmark

Pagination help?

Hi,

I’m having problems with my pagination search results page. I’m getting the message:

[b]Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/***********/results.php on line 38[/b]

Which is part of this line:

[code=php]
$query_count = mysql_query ( “SELECT COUNT(*) AS total FROM news_stories WHERE ” . $search_query );
$result_count = mysql_fetch_assoc ( $query_count );
[/code]

However, I can’t see what is wrong? Is it not seeing my $search_query and paginating the results?

[code=php] $search_query = “select *, DATE_FORMAT(appeared, ‘%W %d %M, %Y’) as published_date from news_stories where section like ‘%$section%’ AND unix_timestamp(published) <= unix_timestamp(NOW()) AND ( headline LIKE ‘%$searchstring%’ OR story_text LIKE ‘%$searchstring%’ ) Order by id Asc” or die(mysql_error()); [/code]

Can someone please help?

Thanks

Chris

[code=php]
<?php

// Start the connection to the database

include(“************.ini”);

// End the connection to the database

// Start to get the data from the form and trim any whitespace

if($_SERVER[“REQUEST_METHOD”]==’POST’)
{
$section = trim($_POST[‘section’]);
$searchstring = ($_POST[‘searchstring’] != “”) ? $_POST[‘searchstring’] : false;

}
else
{
$section = trim($_GET[‘section’]);
$searchstring = trim ($_GET[‘searchstring’] != “”) ? $_GET[‘searchstring’] : false;

}

// End getting the data from the form and trimming any whitespace

// Start to build the query

$search_query = “select *, DATE_FORMAT(appeared, ‘%W %d %M, %Y’) as published_date from news_stories where section like ‘%$section%’ AND unix_timestamp(published) <= unix_timestamp(NOW()) AND ( headline LIKE ‘%$searchstring%’ OR story_text LIKE ‘%$searchstring%’ ) Order by id Asc” or die(mysql_error());

// End building the query

// Start pagination script and state amount of records per page

$limit = 5;
$query_count = mysql_query ( “SELECT COUNT(*) AS total FROM news_stories WHERE ” . $search_query );
$result_count = mysql_fetch_assoc ( $query_count );
$totalrows = $result_count[‘total’];
$PHP_SELF = $_SERVER[‘PHP_SELF’];

if( ! isset ( $_GET[‘page’] ) )
{
$page = 1;
}
else
{
$page = $_GET[‘page’];
}

$limitvalue = $page * $limit – ($limit);

// End pagination script and state amount of records per page

// Start to find how many search results are being found for the query

$search_query . ” LIMIT ” . $limitvalue . “, ” . $limit;
$search_results = mysql_query($search_query, $link);
$result = mysql_query($search_query) or die (mysql_error());

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query(“SELECT FOUND_ROWS()”), 0);

if($total_results <= 0)
{
echo “Sorry, there were no results for your search.”;
}

// Else and Start to find how many pagination pages I have
else
{
echo “Your search returned “.$totalrows.” result(s). <br /><br />Here are those results, listed in ascendng order. <br /><br />”;

if($page != 1){
$pageprev = $page – 1;

echo(“<a href=”$PHP_SELF?page=$pageprev”><img src=”results/previous.jpg” width=”120″ height=”22″ class=”prev_next_border”></a> “);
}else{
echo(“”);
}

$numofpages = $number_of_results/ $limit;

#echo “<br>”, $totalrows;
#exit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i.” “);
}else{
echo(“<a href=”$PHP_SELF?page=$i”>$i</a> “);
}
}

if(($totalrows – ($limit * $page)) > 0){
$pagenext = $page + 1;

echo(“<a href=”$PHP_SELF?page=$pagenext”><img src=”results/next.jpg” width=”120″ height=”22″ class=”prev_next_border”></a>”);
}else{
echo(“”);
}

} // End of how many results I have found

mysql_free_result($result);

// End of Else and to find how many pagination pages I have

?>
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 — At a minimum, make the following changes:

From (1):[code=php]
$search_query = "select *, DATE_FORMAT(appeared, '%W %d %M, %Y') as published_date from news_stories where section like '%$section%' AND unix_timestamp(published) <= unix_timestamp(NOW()) AND ( headline LIKE '%$searchstring%' OR story_text LIKE '%$searchstring%' ) Order by id Asc" or die(mysql_error());[/code]
to (1):[code=php]
$search_query = "(select *, DATE_FORMAT(appeared, '%W %d %M, %Y') as published_date from news_stories where section like '%$section%' AND unix_timestamp(published) <= unix_timestamp(NOW()) AND ( headline LIKE '%$searchstring%' OR story_text LIKE '%$searchstring%' ) Order by id Asc)";
[/code]
(Note 1: You're just creating a string to be used as a subquery in a later SELECT statement. It's too early to die() anyways and you can't send die() to the SQL server.)

From (2):[code=php] $query_count = mysql_query ( "SELECT COUNT(*) AS total FROM news_stories WHERE " . $search_query );[/code]to (2)[code=php] $query_count = mysql_query ( "SELECT COUNT(*) AS total FROM news_stories WHERE " . $search_query ) or die(mysql_error(); [/code](Note 2: If you must die(), this is the place to do it.)

I'm not sure the subquery will work as it is, but fix the above and we'll see...
Copy linkTweet thisAlerts:
@bokehJan 08.2007 — [CODE]SELECT COUNT( * ) AS total
FROM images
WHERE (
SELECT * , DATE_FORMAT( appeared, '%W %d %M, %Y' ) AS published_date
FROM news_stories
WHERE section LIKE '%$section%'
AND unix_timestamp( published ) <= unix_timestamp( NOW( ) )
AND (
headline LIKE '%$searchstring%'
OR story_text LIKE '%$searchstring%'
)
ORDER BY id ASC
)[/CODE]
That query looks ok. You need to echo it with the variables expanded. And as Nightshift says: echo the error message.
×

Success!

Help @chrisb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...