MySQL error saying query is empty. I can't see this.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
fredley
Forum Newbie
Posts: 7
Joined: Thu Oct 25, 2007 5:32 am

MySQL error saying query is empty. I can't see this.

Post by fredley »

The code below stops at 'Opening Directory', with the error:
Your Query:
Error: (1064) 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
I don't see how this is even possible, I've checked and rechecked all my references, none of the query strings could possibly be empty! Also in between the two reference points (echoing of Opening Directory and Opening $file) there aren't any SQL Queries! Am I missing something?

Code: Select all

<?php
include_once('lib.php');
include_once('gallerylib.php');
 
mysqlConnect();
$query = "SELECT ID FROM galleryindex ORDER BY ID DESC LIMIT 1";
$result = mysqlQuery($query);
$row = mysql_fetch_assoc($result);
$AID = $row['ID'] + 1;
 
 
$path = 'filedump/';
$dir = opendir($path);
$firstimg = true;
$width = 800;
$height = 800;
$i=1;
$i=1;
while(($file = readdir($dir)) !== false)
    {
    $filetype = filetype($path . $file);
    if($filetype == 'dir' && !($file == '.' || $file == '..' || substr($file,0,4) == 'DONE'))
        {
        echo $file . " found.\n";
        //check new gallery
        $query2 = 'SELECT ID FROM galleryindex WHERE name=\''.addslashes($file).'\'';
        $result2 = mysqlQuery($query2);
        if(mysql_num_rows($result2) == 1)
            {
            //existing
            echo "This gallery exists, appending photos...\n";
            $row2=mysql_fetch_assoc($result2);
            $AID = $row2['ID'];
            }else{
            //new
            $query1 = "INSERT INTO galleryindex (ID,name) VALUES ($AID,'" . addslashes($file) . "')";
            echo $query1;
            $result1 = mysqlQuery($query1);
            echo "Database updated.\n";
            }
        $dir2 = opendir($path . $file);
        echo "Opening Directory\n";
        while(($file2 = readdir($dir2)) !== false)
            {
            $i++;
            $qID = time() . $i;
            if(substr($file2,-3) == 'jpg' || substr($file2,-4) == 'jpeg')
                {
                echo 'Opening '.$file2."\n";
                if($firstimg)
                    {
                    //set cover
                    echo 'Setting Cover'."\n";
                    $firstimg=false;
                    $query3 = "UPDATE galleryindex SET cover=$qID WHERE ID=$AID";
                    echo $query3;
                    $result3 = mysqlQuery($query3);
                    echo "Cover set to: ";
                    }
                list($width_orig, $height_orig) = getimagesize($path . $file . '/' . $file2);
                if($width_orig > $width || $height_orig > $height)
                    {
                    //resize here
                    $image_p = resizeImage($width_orig,$height_orig,$width,$height,$path . $file . '/' . $file2);
                    imagejpeg($image_p, 'gallery/images/' . $qID . '.jpg', 100);
                    $image_p = resizeImage($width_orig,$height_orig,100,150,$path . $file . '/' . $file2);
                    imagejpeg($image_p,'gallery/images/' . $qID . 'thumb.jpg', 100);
                    echo $file2 . " added and resized.\n";
                    //resizing done
                    }else{
                    //original does not need resizing
                    $image_p = resizeImage($width_orig,$height_orig,100,150,$path . $file . '/' . $file2);
                    imagejpeg($image_p,'gallery/images/' . $qID . 'thumb.jpg', 100);
                    move_uploaded_file($path . $file . '/' . $file2, 'gallery/images/' . $qID . ".jpg");
                    echo $file2 . " added but not resized.\n";
                    //all done
                    }
                //echo "Temp file " . $file2 . " deleted. \n";
                $values .= "('$AID','$qID'),";
                }
            }
        closedir($dir2);
        }
    }
closedir($dir);
$query = "INSERT INTO gallerycontents (AID,path) VALUES " . substr($values,0,-1);
$result = mysqlQuery($query);
galleryrss($AID);
?>
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MySQL error saying query is empty. I can't see this.

Post by Christopher »

I don't think that error means it is an empty string. Which query is giving the error? Is is $query3 or the INSERT at the bottom?
(#10850)
fredley
Forum Newbie
Posts: 7
Joined: Thu Oct 25, 2007 5:32 am

Re: MySQL error saying query is empty. I can't see this.

Post by fredley »

My die() includes the full query string, as you can see from the response quoted, it is empty. I'm assuming it's $query3, but then the script does not echo 'Setting Cover', which is before this. As $firstimg is true to start off with, the script could not get to the final INSERT without going through this, unless the folder was empty (which it's not). A thought, the name of my directory contains spaces, could this be causing problems? Do I need to format $dir2 in readdir($dir2) in order for this to work correctly?

Tom
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MySQL error saying query is empty. I can't see this.

Post by Christopher »

You might want to print $query SQL because the values may not be right:

Code: Select all

$query = "INSERT INTO gallerycontents (AID,path) VALUES " . substr($values,0,-1);
(#10850)
fredley
Forum Newbie
Posts: 7
Joined: Thu Oct 25, 2007 5:32 am

Re: MySQL error saying query is empty. I can't see this.

Post by fredley »

OK, the problem does seem to be with $query, but why is the previous query not being reached?
Post Reply