darkangel2001lv;10998627 wrote:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/84/8829884/html/members/search3.php on line 190
This question has been answered several times on this forum. Each call to [man]mysql_query[/man] returns a result set resource OR boolean false. [man]mysql_fetch_assoc[/man] only ever takes a result set resource argument, never boolean false as the documentation tells you.
Use mysql_errno and mysql_error to see what went wrong.
As for your code, I recommend you read an article about indentation, for example on wikipedia. Choose something, whatever you wish and stick to it. But I would claim that you really need to keep your indentation level at the same depth for all statements belonging to the same block.
Thus
if (true)
# This starts a block
{
# All statements that belong to that block should be at the same indent depth
mysql_query(); // Like this one
mysql_fetch_assoc(); // and this one
foreach(); // this one is NOT in the correct place
foreach() // this is where it should be
# new block opened here
{
# Thus, indent one more level
etc();
}
}
If you look at your code below, suddenly everything turns red. If you look at the beginning of the code, only string literals are red. Thus, at some point, you turn all (most) the remaining code into a string literal. Find the extra/missing single or double quote to fix it.
/* Setup vars for query. */
$targetpage = "$_SERVER['PHP_SELF']"'; // <------------------------WANT TO TARGET SAME PAGE NOT SURE IF I HAVE THIS CORRECT GET SYNTAX ERROR THIS LINE
$limit = 5; //how many items to show per page
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
if ($_POST['search']=="search")
{
$keyword=$_POST['keyword'];
$result=mysql_query("SELECT link, keywords FROM products WHERE keywords LIKE '%$keyword%' OR `url` LIKE '%$keyword%' LIMIT $start, $limit");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "The search term provided {$searchTerms} yielded no results.";
}else {
$results = array(); // the result array
$i = 1;
}
}
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Apply rules and draw the pagination object.
Saving the code to a variable in case need to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
else
$pagination.= "<span class=\"disabled\">« previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
else
$pagination.= "<span class=\"disabled\">next »</span>";
$pagination.= "</div>\n";
}
?>
<?php
while ($row = mysql_fetch_assoc($results)) { // <-----------|Know part of problem is this is incorrect
$results[] = " {$row['link']}<br />"; // <-----------|
}
?>
<?=$pagination?>
part of code I know is not right:
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
else
$pagination.= "<span class=\"disabled\">next »</span>";
$pagination.= "</div>\n";
}
?>
<?php
while ($row = mysql_fetch_assoc($results)) { // <-----------|Know part of problem is this is incorrect This is line 190 where I get syntax error message
$results[] = " {$row['link']}<br />"; // <-----------|
}
?>
<?=$pagination?>
And a side question am I doing this correct?:
/* Setup vars for query. */
$targetpage = "$_SERVER['PHP_SELF']" // <------------------------WANT TO TARGET SAME PAGE NOT SURE IF I HAVE THIS CORRECT GET SYNTAX ERROR THIS LINE AS WELL
$limit = 5; //how many items to show per page
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0