Page 1 of 1

Parse Error, that i can't seem to find.

Posted: Wed Apr 21, 2004 4:07 pm
by Pizentios
Hey,

i have been punding my head agains this parse error for about a day now, and have come no closer to solving the problem. I thought that i might post it here and see if anybody had any ideas. the error is:

Warning: pg_query(): Query failed: ERROR: syntax error at or near ";" at character 31 . in /home/nick/include/dbobj.php on line 157

so i go and look at line 157 heres a code snipet of the function that it is in:

Code: Select all

function dbSelect($what, $from, $where)
        {
        /////////////////////////////////////////////////////////////////////////////////////
        // Purpose: This function lets you do a select from the db that you just connected //
        //           too.                                                                  //
        // Pre: The connection has to be made, also you need to send at least the what     //
        //      and from vars for this function to run.                                    //
        //      Example: if you wanted to select all cats from the table called cats:      //
        //               dbSelect("*", "cats", "");                                        //
        //               if you wanted to select all the cats from the table called cats   //
        //               that are red:                                                     //
        //               dbSelect("*", "cats", "color=red");                               //
        // Post: Returns the record set. Returns false if it blew up.                      //
        // Date Created: 04/21/04                                                          //
        // Date Finished: 04/21/04                                                         //
        /////////////////////////////////////////////////////////////////////////////////////

                if ($where = "") //if where is equl to nothing, then built the sql without it.
                {
                        $sql = "SELECT {$what} FROM {$from};";
                }
                else //else sql includes the where clause.
                {
                        $sql = "SELECT {$what} FROM {$from} WHERE {$where};";
                }
                echo $sql;
                $res = pg_query($sql);

                if ($res <> false)
                {
                        return $res;
                }
                else
                {
                        return false;
                }
        }
anyways, line 157 is the line where i run the query (ie: pg_query). Can anybody see the problem?? Thanks in advance.

Posted: Wed Apr 21, 2004 4:25 pm
by JAM
Impossible, As we cant see what the variables using within the query is like.
You echo $sql, what does that say?

And by looking at the comments at the top:
// if you wanted to select all the cats from the table called cats //
// that are red: //
// dbSelect("*", "cats", "color=red"); //
This part of that, is likely wrong.

Code: Select all

dbSelect("*", "cats", "color=red");
...should be...

Code: Select all

dbSelect("*", "cats", "color='red'");
...if you follow. Single-quotes around values.

Posted: Wed Apr 21, 2004 4:35 pm
by Pizentios
I am a complete idiot. you are right, the prolem is with the sql, but not with the values that i am sending it. the problem is where i am building the sql statment from the values...specifically, the first part of the if statment.

Code: Select all

if ($where = "") //if where is equl to nothing, then built the sql without it. 
                { 
                        $sql = "SELECT {$what} FROM {$from};"; 
                } 
                else //else sql includes the where clause. 
                { 
                        $sql = "SELECT {$what} FROM {$from} WHERE {$where};"; 
                }
the "if ($where = "")" part seems to be getting run, instead of skiping to the else part. i fixed that part, thanks for giving me the boot in the head that i needed.

Posted: Wed Apr 21, 2004 4:38 pm
by JAM

Code: Select all

$sql = "SELECT &#123;$what&#125; FROM &#123;$from&#125;;";
Might be an issue also as

Code: Select all

$sql = "SELECT &#123;$what&#125; FROM &#123;$from&#125;";
...looks much better. Note the ;
Applies in both select-clauses...

Yah, I missed that single = as usual. The easiest things are the hardest to spot... =)

Posted: Wed Apr 21, 2004 4:54 pm
by Pizentios
no kidding, i am just glad that i could get this cleared up before i went home for the day. Otherwise it would be bugging me all night...

Posted: Wed Apr 21, 2004 5:41 pm
by feyd
== not =. = is assignment. == is checking.

Finding Parsing Errors - Charlie's Method

Posted: Wed Apr 21, 2004 7:38 pm
by chvol
Anytime I get a tough parse error, I break the line a bunch of times (insert line feeds) at delimiters (e.g. commas) so the line it happens on is just a few characters!

Charlie

Posted: Wed Apr 21, 2004 8:36 pm
by John Cartwright
if ($where = "")

should be

if ($where == "")

or

if (empty($where))

Posted: Wed Apr 21, 2004 10:28 pm
by Illusionist
I think you may be a little late Phenom... They've already pointed that out. :)