#1064 - You have an error in your SQL syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Supremezzy
Forum Newbie
Posts: 8
Joined: Thu Jan 26, 2012 1:32 pm

#1064 - You have an error in your SQL syntax

Post by Supremezzy »

Hello All, I Recently tried to Host to a new hosting service for an old database, but i am having some trouble and errors.
Some Pages Come up with an error saying "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"

Here is one of the pages that get this error.

Code: Select all

 <?php
	  $GalleryID=$_GET['ID']; 
//      $sql_rsGallery = "SELECT * FROM tGallery WHERE GalleryID=1";
      $sql_rsGallery = "SELECT * FROM tGallery WHERE GalleryID=$GalleryID";
      $rsGallery = mysql_query($sql_rsGallery, $conDB) or die(mysql_error());
      $totalRows_rsGallery = mysql_num_rows($rsGallery);
      if ($totalRows_rsGallery > 0) {
		$row_rsGallery = mysql_fetch_assoc($rsGallery);
	    echo $row_rsGallery['GalleryTitle3'];
		echo "<br /><br /><br />";
		$GalleryFolder = $row_rsGallery['GalleryFolder'];
		foreach (new DirectoryIterator('gallery/'.$GalleryFolder.'/small/') as $file) {
		  // if the file is not this file, and does not start with a '.' or '..',
		  // then store it for later display
		  $FirstChar = $file->getFilename();
		  $FirstChar = $FirstChar[0];
		  if ( ($FirstChar != ".") && (pathinfo($file, PATHINFO_EXTENSION) == "jpg") && (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) {
			// if the element is a directory -> dont use it
			$arrFiles[] = ($file->isDir()) ? $file="" : $file->getFilename();
		  }
		}
		//print_r($arrFiles);
		asort($arrFiles);
		foreach ($arrFiles as $PictureName) {
		  if ($GalleryFolder == "dancing") { // Gallery dancing are Portrait, so width and height are different
			?>
			<a href="gallery/<?php echo $row_rsGallery['GalleryFolder']; ?>/big/<?php echo $PictureName; ?>" rel="lightbox[salsa1]" title=""><img src="gallery/<?php echo $row_rsGallery['GalleryFolder']; ?>/small/<?php echo $PictureName; ?>" title="Click to enlarge" alt="" width="100" height="150" /></a>
			<?php
		  }
		  else {
			?>
			<a href="gallery/<?php echo $row_rsGallery['GalleryFolder']; ?>/big/<?php echo $PictureName; ?>" rel="lightbox[salsa1]" title="<a href="gallery/<?php echo $row_rsGallery['GalleryFolder']; ?>/download/<?php echo $PictureName; ?>" target="_blank">Download this picture &raquo;</a>"><img src="gallery/<?php echo $row_rsGallery['GalleryFolder']; ?>/small/<?php echo $PictureName; ?>" title="Click to enlarge" alt="" width="133" height="100" /></a>
			<?php
		  }
		}
	  }
	  mysql_free_result($rsGallery);
	  ?> 
Any Idea's on how to fix this?
Thanks
Last edited by Supremezzy on Thu Jan 26, 2012 2:07 pm, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: #1064 - You have an error in your SQL syntax

Post by Celauran »

I think your post is broken.
Supremezzy
Forum Newbie
Posts: 8
Joined: Thu Jan 26, 2012 1:32 pm

Re: #1064 - You have an error in your SQL syntax

Post by Supremezzy »

Can anyone help, i really need to fix this website...
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: #1064 - You have an error in your SQL syntax

Post by Celauran »

Supremezzy wrote:Some Pages Come up with an error saying "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"
Have you tried to echo the query on the pages that fail?
Supremezzy
Forum Newbie
Posts: 8
Joined: Thu Jan 26, 2012 1:32 pm

Re: #1064 - You have an error in your SQL syntax

Post by Supremezzy »

Celauran wrote:Have you tried to echo the query on the pages that fail?
How do you echo the query exactly?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: #1064 - You have an error in your SQL syntax

Post by Celauran »

Code: Select all

echo $sql_rsGallery;
Supremezzy
Forum Newbie
Posts: 8
Joined: Thu Jan 26, 2012 1:32 pm

Re: #1064 - You have an error in your SQL syntax

Post by Supremezzy »

Ok i added that line. Now what?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: #1064 - You have an error in your SQL syntax

Post by Celauran »

When you get an error, you will see the query that caused the error as it was sent to the SQL server. From there you can debug further.
Supremezzy
Forum Newbie
Posts: 8
Joined: Thu Jan 26, 2012 1:32 pm

Re: #1064 - You have an error in your SQL syntax

Post by Supremezzy »

How do i access the SQL server?
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Re: #1064 - You have an error in your SQL syntax

Post by php3ch0 »

Add this line in...

Code: Select all

       $sql_rsGallery = "SELECT * FROM tGallery WHERE GalleryID=$GalleryID";
die(  $sql_rsGallery);
       $rsGallery = mysql_query($sql_rsGallery, $conDB) or die(mysql_error());
Then you should see the sql in your browser. If you can copy and paste it here we can have a look. Or it might become obvious. e.g empty $GalleryID
Supremezzy
Forum Newbie
Posts: 8
Joined: Thu Jan 26, 2012 1:32 pm

Re: #1064 - You have an error in your SQL syntax

Post by Supremezzy »

Ok i added the code and the web page comes up with this.

SELECT * FROM tGallery WHERE GalleryID=
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: #1064 - You have an error in your SQL syntax

Post by egg82 »

first, I recommend adding a backquote (`) before and after a table, database, and column name (ie. SELECT * FROM `tGallery` WHERE `GalleryID`=)

second, I recommend reading this: http://www.amazon.com/PHP-MySQL-Dummies ... 0470527587
I learned a LOT from it
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Re: #1064 - You have an error in your SQL syntax

Post by php3ch0 »

It looks like the variable $GalleryID=$_GET['ID']; is empty so when it is sending the data to the query it looks like SELECT * FROM tGallery WHERE GalleryID= when it should be something like SELECT * FROM tGallery WHERE GalleryID=4
Post Reply