Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Bottom
$dbconn -> Execute($sql) not working...
  • Posted: 27.07.2005, 00:12
     
    uheweb
    rank:
    Professional Professional
    registered:
     September 2004
    Status:
    offline
    last visit:
    10.11.08
    Posts:
    815
    I'm using the following in my getall() function of the pnuserapi:

    Code

    function Itinerary_userapi_getall($args)
    {
        // Get arguments from argument array - all arguments to this function
        // should be obtained from the $args array, getting them from other
        // places such as the environment is not allowed, as that makes
        // assumptions that will not hold in future versions of PostNuke
        extract($args);

        // Optional arguments.
        if (!isset($see)) {
            $see = 'events';
        }

        $items = array();

        // Security check - important to do this as early on as possible to
        // avoid potential security holes or just too much wasted processing
        if (!pnSecAuthAction(0, 'Itinerary::', '::', ACCESS_OVERVIEW)) {
            return $items;
        }

        // Get datbase setup - note that both pnDBGetConn() and pnDBGetTables()
        // return arrays but we handle them differently.  For pnDBGetConn() we
        // currently just want the first item, which is the official database
        // handle.  For pnDBGetTables() we want to keep the entire tables array
        // together for easy reference later on
        $dbconn =& pnDBGetConn(true);
        $pntable =& pnDBGetTables();
        $sql = '';

        // It's good practice to name the table and column definitions you are
        // getting - $table and $column don't cut it in more complex modules
        $Itineraryetable  = $pntable['itinerary_events'];
        $Itineraryecolumn = &$pntable['itin_events_column'];
        $Itineraryitable  = $pntable['itinerary_items'];
        $Itineraryicolumn = &$pntable['itin_items_column'];
        $Itineraryttable  = $pntable['itinerary_types'];
        $Itinerarytcolumn = &$pntable['itin_types_column'];
        $Itineraryctable  = $pntable['itinerary_config'];
        $Itineraryccolumn = &$pntable['itin_config_column'];
        $Itineraryatable  = $pntable['itinerary_address'];
        $Itineraryacolumn = &$pntable['itin_address_column'];
        $Itineraryutable  = $pntable['itinerary_users'];
        $Itineraryucolumn = &$pntable['itin_users_column'];
       

        // Get items - the formatting here is not mandatory, but it does make the
        // SQL statement relatively easy to read.  Also, separating out the sql
        // statement from the SelectLimit() command allows for simpler debug
        // operation if it is ever needed
       
        $sql = "SELECT $Itineraryecolumn[eid],
                       $Itineraryecolumn[ename],
                       $Itineraryecolumn[edesc],
                       $Itineraryecolumn[estart],
                       $Itineraryecolumn[eend]
                FROM $Itineraryetable
                ORDER BY $Itineraryecolumn[estart]"
    ;

        echo $sql;

        $result =& $dbconn->Execute($sql);
       

        // Check for an error with the database code, and if so set an appropriate
        // error message and return
        if ($dbconn->ErrorNo() != 0) {
            pnSessionSetVar('errormsg', _GETFAILED);
            return false;
        }

        // Put items into result array.  Note that each item is checked
        // individually to ensure that the user is allowed access to it before it
        // is added to the results array
        for (; !$result->EOF; $result->MoveNext()) {
            list($eid, $ename, $edesc, $estart, $eend) = $result->fields;
            if (pnSecAuthAction(0, 'Itinerary::', "$ename::$eid", ACCESS_OVERVIEW)) {
                $items[] = array('eid' => $eid,
                                 'ename' => $ename,
                                 'edesc' => $edesc,
                                 'estart' => $estart,
                                 'eend' => $eend);
            }
        }

        // All successful database queries produce a result set, and that result
        // set should be closed when it has been finished with
        $result->Close();

        // Return the items
        return $items;
    }


    On executing, I get this error (notice that the value for $SQL is echoed before its execution so I can see the formed SQL statement):

    Code

    SELECT itin_eventid, itin_eventname, itin_eventdesc, itin_eventstart, itin_eventend FROM pn_itinerary_events ORDER BY itin_eventstart
    Fatal error: mysql 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 ' , FROM' at line 1] in EXECUTE("SELECT , , FROM ") in f:\ncm\www\fsintra\includes\classes\adodb\adodb-errorhandler.inc.php on line 153


    So, why would the $SQL now be stripped of everything but "SELECT , , FROM"??

    An interestng item... I put this in place of $SQL in the $result =& $dbconn->Execute($SQL);:

    Code

    $result =& $dbconn->Execute('SELECT itin_eventid, itin_eventname, itin_eventdesc, itin_eventstart, itin_eventend FROM pn_itinerary_events ORDER BY itin_eventstart');


    This also returns the same error, BUT if I break the SQL (bad table name for example) like so:

    Code

    $result =& $dbconn->Execute('SELECT itin_eventid, itin_eventname, itin_eventdesc, itin_eventstart, itin_eventend FROM pn_itinerary_events ORDER BY itin_BADTABLENAME');


    Then, it returns the SQL in the error code like:

    Code

    Fatal error: mysql error: [1054: Unknown column 'itin_BADTABLENAME' in 'order clause'] in EXECUTE("SELECT itin_eventid, itin_eventname, itin_eventdesc, itin_eventstart, itin_eventend FROM pn_itinerary_events ORDER BY itin_BADTABLENAME") in f:\ncm\www\fsintra\includes\classes\adodb\adodb-errorhandler.inc.php on line 153


    So why, with the correctly formed SQL does the query revert to "SELECT , , FROM" ???

    Anyone seen this before? Should I use a different method? I'm just following the example module DB calls.

    Thanks,

    Nolan M.
    UHEweb
  • Posted: 27.07.2005, 01:55
     
    uheweb
    rank:
    Professional Professional
    registered:
     September 2004
    Status:
    offline
    last visit:
    10.11.08
    Posts:
    815
    SOLVED - the error wasn't referring to the above $SQL code, but the next in the main user function, the countitems WHICH I HAVEN'T DEFINED YET icon_smile

    Commenting out THAT pnModAPIFunc call allows the above code to execute fine.

    UHEweb
  • Posted: 27.07.2005, 17:21
     
    cannibus
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    20.09.08
    Posts:
    401
    glad to see you got it!
  • Posted: 27.07.2005, 18:24
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    Thanks for posting your resolution.

    :)

    --
    Photography | PHP | Other

Extensions Moderation

Main Menu

Extensions Database

Documentation

Development

Login

Donate to Zikula