Jump to content

Pagination


raptor30506090

Recommended Posts

Hi every one

 

im getting an error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

SELECT * FROM

 

if(isset($_GET['subid'])){
                      $id = $_GET['subid'];
					}						
                     $query = mysql_query("SELECT * FROM sub_nav WHERE subNav_ID=" . mysql_real_escape_string((int)$id));
					 $row = mysql_fetch_assoc($query);
					 $table =  strtolower($row['subNavName']);
					 // ******************************* This if statment controls the content ************************
					 if($row['show_hide'] == 1){
						                      $per_page = 2;

$pages_query = mysql_query("SELECT COUNT('id') FROM textiles");

$pages = ceil(mysql_result($pages_query, 0) / $per_page);

$page = (isset($_GET['p']) AND (int)$_GET['p'] > 0) ? (int)$_GET['p'] : 1;
  
$start = ($page-1) * $per_page;

$query = mysql_query("SELECT imageName FROM textiles LIMIT $start, $per_page");





while ($query_row = mysql_fetch_assoc($query)){
echo '<p>',$query_row['imageName'] , '</p>';
}


if ($pages >= 1){

	for($x=1; $x<=$pages; $x++){
		echo '<a href="?p='.$x.'">'.$x.'</a> '; 
		}
	}

 

Can any one help please

Link to comment
Share on other sites

When running queries - especially dynamic ones (i.e. they have variables) I would highly encourage you to create the queries as string variables first. That way if there is an error you can echo the query out for debugging purposes.

 

Also, there is absolutely no reason to use this:

mysql_real_escape_string((int)$id)

If you have forced the value to be an integer, then mysql_real_escape_string() is not doing anything of value. using (int) is already making the value safe for a DB query

 

Based upon the partial error you posted the only query which that would come from is this one:

$query = mysql_query("SELECT * FROM sub_nav WHERE subNav_ID=" . mysql_real_escape_string((int)$id));

 

BUt, you don't have any error handling that would have displayed that error! Are you sure that is the correct code that is producing the error? Try changing to the following

$id = (int) $id;
$sql = "SELECT * FROM sub_nav WHERE subNav_ID = {$id}";
$query = mysql_query($sql);
if(!$query)
{
    die("Query: {$sql}<br>Error: " . mysql_error());
}

Link to comment
Share on other sites

You should echo out this query:

 

"SELECT imageName FROM textiles LIMIT $start, $per_page"

 

And post what it outputs. I'm assuming that's where the error is occurring.

 

 

 

All of Psychos advice is great, and escaping the variable after using (int) IS redundant, though I don't think it should cause an error.

Link to comment
Share on other sites

Hi all thank you for your help yes jesirose that the paganation link iv tryed the above with no luck

 

still getting the same error

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

SELECT * FROM

Link to comment
Share on other sites

Hi here is the new code

 

if(isset($_GET['subid'])){
                      $id = $_GET['subid'];
					}



$id = (int) $id;
$sql = "SELECT * FROM sub_nav WHERE subNav_ID = {$id}";
$query = mysql_query($sql);
if(!$query)
{
    die("Query: {$sql}<br>Error: " . mysql_error());
}



					 $row = mysql_fetch_assoc($query);
					 $table =  strtolower($row['subNavName']);
					 // ******************************* This if statment controls the content ************************
					 if($row['show_hide'] == 1){
$per_page = 2;

$pages_query = mysql_query("SELECT COUNT('id') FROM textiles");

$pages = ceil(mysql_result($pages_query, 0) / $per_page);

$page = (isset($_GET['p']) AND (int)$_GET['p'] > 0) ? (int)$_GET['p'] : 1;
  
$start = ($page-1) * $per_page;

$query = mysql_query("SELECT imageName FROM textiles LIMIT $start, $per_page");

while ($query_row = mysql_fetch_assoc($query)){
echo '<p>',$query_row['imageName'] , '</p>';
}


if ($pages >= 1){

	for($x=1; $x<=$pages; $x++){
		echo '<a href="?p='.$x.'">'.$x.'</a> '; 
		}
	}


											   }

 

Thank you

Link to comment
Share on other sites

Can you copy and paste the error directly? With the 'Query: ' and 'Error: ' strings included?

It's odd your query is being truncated to SELECT * FROM, if that's indeed what's happening.

 

Also, don't bump your threads until they've dropped off the first page, it's against the rules and will discourage help. We find it rude and selfish.

Link to comment
Share on other sites

ok this incudes the else

 


<?php
if(isset($_GET['subid'])){
                      $id = $_GET['subid'];
					}



$id = (int) $id;
$sql = "SELECT * FROM sub_nav WHERE subNav_ID = {$id}";
$query = mysql_query($sql);
if(!$query)
{
    die("Query: {$sql}<br>Error: " . mysql_error());
}



					 $row = mysql_fetch_assoc($query);
					 $table =  strtolower($row['subNavName']);
					 // ******************************* This if statment controls the content ************************
					 if($row['show_hide'] == 1){
$per_page = 2;

$pages_query = mysql_query("SELECT COUNT('id') FROM textiles");

$pages = ceil(mysql_result($pages_query, 0) / $per_page);

$page = (isset($_GET['p']) AND (int)$_GET['p'] > 0) ? (int)$_GET['p'] : 1;
  
$start = ($page-1) * $per_page;

$query = mysql_query("SELECT imageName FROM textiles LIMIT $start, $per_page");

while ($query_row = mysql_fetch_assoc($query)){
echo '<p>',$query_row['imageName'] , '</p>';
}


if ($pages >= 1){

	for($x=1; $x<=$pages; $x++){
		echo '<a href="?p='.$x.'">'.$x.'</a> '; 
		}
	}


											   }else{  
												      if(isset($_GET['subid'])){
                                                                                $id = $_GET['subid'];
					                                                        }						
                                                                            $query = mysql_query("SELECT * FROM sub_nav WHERE subNav_ID=" . (int)$id);
					                                                        $row = mysql_fetch_assoc($query);
					                                                        $table =  strtolower($row['subNavName']);
						                                                    $sql = "SELECT * FROM $table";

																			$query = mysql_query($sql) or die(mysql_error() . '<br />' . $sql);
																			$row = mysql_fetch_assoc($query);
																			echo '<p>'.$row['content'].'</p>';
												                            }

?>

hope this helps if i remove the else then it returns nothing

Thanks guys and girls


Link to comment
Share on other sites

$table is undefined.

 

You need to make sure your data contains what you expect (or even exists) before using it in a query.

 

You should code with error reporting set to -1. See my signature. Your code would have generated at the very least an undefined index notice.

Link to comment
Share on other sites

$table is undefined.

 

You need to make sure your data contains what you expect (or even exists) before using it in a query.

 

You should code with error reporting set to -1. See my signature. Your code would have generated at the very least an undefined index notice.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.