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

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
Pizentios
Forum Newbie
Posts: 4
Joined: Wed Apr 21, 2004 4:07 pm
Location: Brandon, Manitoba, Canada

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

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Pizentios
Forum Newbie
Posts: 4
Joined: Wed Apr 21, 2004 4:07 pm
Location: Brandon, Manitoba, Canada

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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... =)
Pizentios
Forum Newbie
Posts: 4
Joined: Wed Apr 21, 2004 4:07 pm
Location: Brandon, Manitoba, Canada

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

== not =. = is assignment. == is checking.
chvol
Forum Newbie
Posts: 20
Joined: Fri Apr 26, 2002 2:49 pm

Finding Parsing Errors - Charlie's Method

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

if ($where = "")

should be

if ($where == "")

or

if (empty($where))
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

I think you may be a little late Phenom... They've already pointed that out. :)
Post Reply