Jump to content

Recommended Posts

I'm getting a syntax error on this line because of the CONCAT function and I'm trying to figure out where the ending ) needs to be because I think that's the issue here.

 

$this->db->select("CONCAT_WS(' ', ".$this->master_model->users_table.".first_name", $this->master_model->users_table.".last_name) AS author", FALSE);

Link to comment
https://forums.phpfreaks.com/topic/268334-syntax-error-of-in-the-worng-place/
Share on other sites

The problem is probably the placement of your comma. However you really should know by now and I've seen you be told: Post the exact entire error, and post the section of code not just one line, since errors can be from the previous line.

Yes I'm sorry.

 

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 (`news_articles`) JOIN `users` ON `users`.`user_id` =`news_articles`.`news_' at line 2

 

SELECT `news_articles`.`news_article_id`, `news_articles`.`news_article_title`, DATE_FORMAT(news_articles.news_article_posting_date, '%M %D, %Y') AS news_article_posting_date, CONCAT_WS(' ', `users`.`first_name`, `news_article_categories`.`news_article_category_name` FROM (`news_articles`) JOIN `users` ON `users`.`user_id` =`news_articles`.`news_article_author_id` JOIN `news_article_categories` ON `news_article_categories`.`news_article_category_id` =`news_articles`.`news_article_category_id`

*headdesk*

 

You still have a comma where there shouldn't be one.

 

What do you *think* the code you've posted does. Why are you sending 3 arguments to that function? Why doesn't the last_name as author part show up in your final query?

Where is the last parenthesis of this:

 

SELECT `news_articles`.`news_article_id`, `news_articles`.`news_article_title`, DATE_FORMAT(news_articles.news_article_posting_date, '%M %D, %Y') AS news_article_posting_date, CONCAT_WS(' ', `users`.`first_name`, `news_article_categories`.`news_article_category_name` FROM (`news_articles`) JOIN `users` ON `users`.`user_id` =`news_articles`.`news_article_author_id` JOIN `news_article_categories` ON `news_article_categories`.`news_article_category_id` =`news_articles`.`news_article_category_id`

$this->db->select("CONCAT_WS(' ', ".$this->master_model->users_table.".first_name" $this->master_model->users_table.".last_name) AS author", FALSE);

 

I'm sorry if I'm being a problem but I honestly don't see the issue. I removed the comma that I think it is.

I think it would help if you posted all the lines of code that are building this query (i.e. the final query you posted doesn't match the fragment you have been posting.)

 

Edit: For the fragment you have been posting -

 

$this->db->select("CONCAT_WS(' ',{$this->master_model->users_table}.first_name, {$this->master_model->users_table}.last_name) AS author", FALSE);

 

$this->db->select($this->master_model->news_articles_table.'.news_article_id');
        $this->db->select($this->master_model->news_articles_table.'.news_article_title');
        $this->db->select("DATE_FORMAT(".$this->master_model->news_articles_table.".news_article_posting_date, '%M %D, %Y') AS news_article_posting_date", FALSE);
        $this->db->select("CONCAT_WS(' ', ".$this->master_model->users_table.".first_name" $this->master_model->users_table.".last_name) AS author", FALSE);
        $this->db->select($this->master_model->news_article_categories_table.'.news_article_category_name');
        $this->db->from($this->master_model->news_articles_table);
        $this->db->join($this->master_model->users_table, $this->master_model->users_table.'.user_id ='.$this->master_model->news_articles_table.'.news_article_author_id');
        $this->db->join($this->master_model->news_article_categories_table, $this->master_model->news_article_categories_table.'.news_article_category_id ='.$this->master_model->news_articles_table.'.news_article_category_id');
        $query = $this->db->get();
        return $query->result();

Also, I edited my post above with some syntax that should work that eliminates all the concatenation that is error prone because it is difficult to see where the php syntax starts and ends where the sql syntax starts and ends (you are missing a comma that is part of the CONCAT_WS() sql syntax and missing some dots that are part of the php concatenation syntax.) It's best to have as few syntax breaks/switches as possible.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.