Okay
  Public Ticket #1758983
Error when searching a MySQL table with a CONCAT() field
Closed

Comments

  •  2
    Carl Nasal started the conversation

    I'm combining fields using CONCAT in a MySQL query like this:

    SELECT db_people.`member_number`,

          concat(
              db_people.`prefix`, ' ',
              db_people.`first_name`, ' ',
              db_people.`middle_name`, ' ',
              db_people.`last_name`, ' ',
              db_people.`suffix`
            ) as `name`,
           db_people.`prefix`, 
           db_people.`first_name`,
           db_people.`middle_name`,
           db_people.`last_name`,
           db_people.`suffix`,
           concat('/person/?people_id=', db_people.`id`) as edit_link
    FROM db_people

    Whenever I do a search (in the search box, not for a specific field) I get an error that says:

    DataTables warning: table id=table_1 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

    When looking at the AJAX response, I see it says:

    <div id="error"><p class="wpdberror"><strong>WordPress database error:</strong> [Unknown column 'db_people.name' in 'where clause']<br /><code>SELECT SQL_CALC_FOUND_ROWS db_people.`member_number`, concat(db_people.`prefix`,' ',db_people.`first_name`,' ',db_people.`middle_name`,' ',db_people.`last_name`,' ',db_people.`suffix`) AS `name`, db_people.`prefix`, db_people.`first_name`, db_people.`middle_name`, db_people.`last_name`, db_people.`suffix`, concat('/person/?people_id=',db_people.`id`) AS edit_link FROM db_people WHERE (`db_people`.`member_number` LIKE '%g%' OR `db_people`.`name` LIKE '%g%' OR `db_people`.`prefix` LIKE '%g%' OR `db_people`.`first_name` LIKE '%g%' OR `db_people`.`middle_name` LIKE '%g%' OR `db_people`.`last_name` LIKE '%g%' OR `db_people`.`suffix` LIKE '%g%' OR `db_people`.`edit_link` LIKE '%g%') ORDER BY `member_number` ASC LIMIT 50</code></p></div>{"draw":2,"recordsTotal":"4104","recordsFiltered":"8","data":[]}

    It looks like you're trying to search for my CONCAT field which is giving an error.

    What I'm trying to achieve is to combine fields (i.e. a person's name or a link using an ID).  Is there a way to do that without CONCAT?  If not, can you fix it so the search doesn't try to search the CONCAT?


    Thanks,
    Carl

  •  1,851
    Miloš replied

    Hi Carl,
    Thank you for your purchase.

    If your MySQL-query based wpDataTable doesn’t work correctly with server-side processing, probably this is happening because wpDataTables server has problems with parsing of the query and building new queries dynamically (happens rarely, but does sometimes). To avoid this please prepare a MySQL view (a stored query), which will return the data that you need, call it e.g. “view1” and then build a wpDataTabled based on a simple query like “SELECT * FROM view1″.

    Please note some this when working with the server-side processing feature:

    • Please do not use “LIMIT” in the SELECT statement. wpDataTables adds it automatically and it will be overridden.
    • Please do not use “ORDER BY” in the SELECT statement. wpDataTables has its own sorting engine so it makes no sense to use MySQL’s sorting, since it will be overridden. Also server-side processing feature adds this part of statement automatically when users triggers the sorting on the front-end, and having it in initial statement may cause the table to crash.


    Best regards.

    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  2
    Carl Nasal replied

    Great tips.  I'll try that.

    Thanks,
    Carl

  •  5
    James Morley replied

    I just had exactly this problem and creating a View solved it!