Jump to:

17452 Posts in 4473 Topics by 1971 members


SilverStripe Forums » Archive » SQLQuery()

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1426 Views
  • thejackel
    Community Member
    33 Posts

    SQLQuery() Link to this post


    I'm trying to generate a custom query so I can specify the date format and possibly group a result set. here is my code:

          $sqlQuery = new SQLQuery();
          $sqlQuery->select = array('YEAR(StartDate) AS Date');
          $sqlQuery->from = "ProjectItem";
          $rawSQL = $sqlQuery->sql();
          $result = $sqlQuery->execute();

    I'm trying to select the StartDate from my 'ProjectItem' table.

    The problem is the query returns an error:

    FATAL ERROR: implode() [function.implode]: Invalid arguments passed

    I'm pretty sure its the 'from' clause, my table exists and the fields are correct.

    Anybody know where i'm going wrong.

    Thanks in advance

  • Sam
    679 Posts

    Re: SQLQuery() Link to this post

    ->select and ->from should be arrays

    Try changing the two lines to this:

    $sqlQuery->select[] = array('YEAR(StartDate) AS Date');
    $sqlQuery->from[] = "ProjectItem";

  • Rahul
    Community Member
    8 Posts

    Re: SQLQuery() Link to this post

    I am trying this query

    $sqlQuery = new SQLQuery();
             $sqlQuery->select = array(
              'ProductName AS ProductName',
              'Industry AS Industry',
              'Type AS Type',
              'Serving AS Serving',
              'Info AS Info',
              'ReviewsBuzz AS ReviewsBuzz',
              'Technology AS Technology',
              'WhatsCooking AS WhatsCooking'
             $sqlQuery->from[] = "
             $sqlQuery->where[] = "
              ID = 1";
    // get the raw SQL
             $rawSQL = $sqlQuery->sql();
             echo $rawSQL;
             // execute and return a Query-object
             return $result = $sqlQuery->execute();

    But it did not return any thing ...
    i tried this as echoing echo $rawSQL;

    SELECT ProductName AS ProductName, Industry AS Industry, Type AS Type, Serving AS Serving, Info AS Info, ReviewsBuzz AS ReviewsBuzz, Technology AS Technology, WhatsCooking AS WhatsCooking FROM SubProducts WHERE ( ID = 1)

    this is working fine in phpmyadmin ...
    Also i tried

    echo "<pre>";
             echo "</pre>";

    it display me

    MySQLQuery Object
    [database:private] => MySQLDatabase Object
    [dbConn:private] => Resource id #89
    [active:private] => 1
    [database:private] => SS_mysite
    [mysqlVersion:private] =>
    [tableList:protected] =>
    [fieldList:protected] =>
    [indexList:protected] =>
    [schemaUpdateTransaction:protected] =>
    [extension_instances:protected] => Array

    [class] => MySQLDatabase

    [handle:private] => Resource id #132
    [currentRecord:private] =>
    [rowNum:private] => -1
    [extension_instances:protected] => Array

    [class] => MySQLQuery

    i am not getting how to get result from this ...
    Please help me ...
    Thanks in Advance

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.