Post by ahmedatwa » Wed Jun 15, 2016 6:26 am

Guys i'm trying to export order table with below function

Code: Select all

protected function getOrders( &$languages, $offset=null, $rows=null, $min_id=null, $max_id=null ) {
		$sql  = "SELECT ";
		$sql .= "  o.order_id,";
		$sql .= "  o.invoice_no,";
		$sql .= "  o.shipping_address_2,";
		$sql .= "  o.shipping_country,";
		$sql .= "  o.shipping_zone,";
		$sql .= "  o.shipping_method,";
		$sql .= "  o.comment,";
		$sql .= "  o.order_status_id,";
		$sql .= "  o.affiliate_id,";
		$sql .= "  o.commission,";
		$sql .= "  o.language_id,";
		$sql .= "  o.accept_language,";
		$sql .= "  o.date_added, ";
		$sql .= "FROM `".DB_PREFIX."order` o ";
	if (isset($min_id) && isset($max_id)) {
			$sql .= "WHERE o.order_id BETWEEN $min_id AND $max_id ";
	}
		$sql .= "GROUP BY o.order_id ";
		$sql .= "ORDER BY o.order_id ASC ";
		if (isset($offset) && isset($rows)) {
			$sql .= "LIMIT $offset,$rows; ";
		} else {
			$sql .= "; ";
		}

        $results = $this->db->query( $sql );
		return $results->rows;
	}
but i'm getting this error

Code: Select all

 Error: 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 `oc_order` o GROUP BY o.order_id ORDER BY o.order_id ASC' at line 1
Error No: 1064

New member

Posts

Joined
Sat Mar 05, 2016 5:22 pm

Post by straightlight » Wed Jun 15, 2016 9:33 am

ahmedatwa wrote:Guys i'm trying to export order table with below function

Code: Select all

protected function getOrders( &$languages, $offset=null, $rows=null, $min_id=null, $max_id=null ) {
		$sql  = "SELECT ";
		$sql .= "  o.order_id,";
		$sql .= "  o.invoice_no,";
		$sql .= "  o.shipping_address_2,";
		$sql .= "  o.shipping_country,";
		$sql .= "  o.shipping_zone,";
		$sql .= "  o.shipping_method,";
		$sql .= "  o.comment,";
		$sql .= "  o.order_status_id,";
		$sql .= "  o.affiliate_id,";
		$sql .= "  o.commission,";
		$sql .= "  o.language_id,";
		$sql .= "  o.accept_language,";
		$sql .= "  o.date_added, ";
		$sql .= "FROM `".DB_PREFIX."order` o ";
	if (isset($min_id) && isset($max_id)) {
			$sql .= "WHERE o.order_id BETWEEN $min_id AND $max_id ";
	}
		$sql .= "GROUP BY o.order_id ";
		$sql .= "ORDER BY o.order_id ASC ";
		if (isset($offset) && isset($rows)) {
			$sql .= "LIMIT $offset,$rows; ";
		} else {
			$sql .= "; ";
		}

        $results = $this->db->query( $sql );
		return $results->rows;
	}
but i'm getting this error

Code: Select all

 Error: 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 `oc_order` o GROUP BY o.order_id ORDER BY o.order_id ASC' at line 1
Error No: 1064

Code: Select all

protected function getOrders( &$languages, $offset=null, $rows=null, $min_id=null, $max_id=null ) {
	$sql = "SELECT `o`.`order_id`, `o`.`invoice_no`, `o`.`shipping_address_2`, `o`.`shipping_country`, `o`.`shipping_zone`, `o`.`shipping_method`, `o`.`comment`, `o`.`order_status_id`, `o`.`affiliate_id`, `o`.`commission`, `o`.`language_id`, `o`.`accept_language`, `o`.`date_added` FROM `" . DB_PREFIX . "order` `o`";
	
	if (isset($min_id) && isset($max_id)) {
			$sql .= "WHERE `o`.`order_id` BETWEEN " . (int)$min_id . " AND " . (int)$max_id;
	}
	
	$sql .= "GROUP BY `o`.`order_id` ";
	
	$sql .= "ORDER BY `o`.`order_id` ASC ";
	
	if (!empty($offset) && !empty($rows)) {
		$sql .= "LIMIT $offset,$rows; ";
	} else {
		$sql .= "; ";
	}

    $results = $this->db->query($sql);
	
	return $results->rows;
}
Two errors spotted; date_added contained a comma before the FROM statement and the comment field which is a reserved word for MySQLi extension.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ahmedatwa » Wed Jun 15, 2016 7:40 pm

Thanks for your reply however even removing those fields didn't solve it

New member

Posts

Joined
Sat Mar 05, 2016 5:22 pm

Post by fido-x » Wed Jun 15, 2016 7:48 pm

This bit here:
ahmedatwa wrote:

Code: Select all

 if (isset($min_id) && isset($max_id)) {
 $sql .= "WHERE o.order_id BETWEEN $min_id AND $max_id ";
 }
If $min_id or $max_id is not set, there is no "WHERE" because the condition is not met. Maybe have an "else", eg:

Code: Select all

 if (isset($min_id) && isset($max_id)) {
 $sql .= "WHERE o.order_id BETWEEN $min_id AND $max_id ";
 } else {
 $sql .= "WHERE o.order_id > '0'";
 }

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by ahmedatwa » Wed Jun 15, 2016 7:59 pm

Thanks Fido-X for reply however i removed the if statement and replaced it with

Code: Select all

 $sql .= "WHERE o.order_id > '0'";
it throws the same sql error

New member

Posts

Joined
Sat Mar 05, 2016 5:22 pm

Post by fido-x » Wed Jun 15, 2016 8:11 pm

Try this:

Code: Select all

     if (isset($min_id) && isset($max_id)) {
     $sql .= "WHERE o.order_id BETWEEN " . $min_id . " AND " . $max_id;
     } else {
     $sql .= "WHERE o.order_id > '0'";
     }
And the other "if" statement:

Code: Select all

      if (isset($offset) && isset($rows)) {
         $sql .= "LIMIT " . $offset,$rows;
      }
NOTE: You do not need an "else" for that last "if" statement.

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia
Who is online

Users browsing this forum: No registered users and 8 guests