• hello hive

    i’m trying to collect variables from a mysql database into one comma-separated string from wordpress using php with limited expertise and your help would be much appreciated

    my database has a table called wp_words_table, with cols: id, words_value, submit_id and item_id

    i want to fetch all words_value data from rows where the submit_id equals 99 (there are many rows with 99 ids, as well as rows with other id numbers)

    the words_value data could be a single word or phrase, or it could be a comma separated string of single words and/or phrases

    i then want to collate this data into one comma separated string ($cloud9). i’ve cobbled the following from what i could glean from the internet, but i get the error: syntax error, unexpected identifier words_value, expecting )

    global $wpdb;
    global $cloud9;
    $cloud9 = $wpdb->CONCAT_WS(‘,’, SELECT words_value FROM wp_words_table WHERE submit_id = 99);

    i’ve tried variations but keep getting the same error. i’m obviously doing something wrong, can anyone see what it is, please?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Imagethreadi

    (@threadi)

    I don’t have your database tables right now, but syntactically correct would be this:

    global $wpdb;
    global $cloud9;
    $cloud9 = $wpdb->get_results('SELECT words_value as listing FROM wp_words_table WHERE submit_id = 99', ARRAY_A);

    If successful, your variable would then contain an array containing the information you are looking for. See also: https://developer.wordpress.org/reference/classes/wpdb/get_results/

    I think if you want to have all of that in one string, you can achieve it like this:

    $liste = implode(',', $cloud9);

    Please note that your question is very much geared towards individual programming. You will probably find more relevant answers here: https://wordpress.stackexchange.com

    Thread Starter Imagechemboy

    (@chemboy)

    hi @threadi thanks for the prompt response

    i inserted your code and tested it and unexpectedly got a return of ARRAY. however i’ll go check out your suggested links and see how i get on

    thanks again

    Moderator Imagebcworkz

    (@bcworkz)

    If you attempted to output $cloud9, such as output to the console log, it very well may just say “Array”. That’s fine and is expected. Once $cloud9 is imploded and and assigned to $liste, then you should get a comma separated list of values.

    Thread Starter Imagechemboy

    (@chemboy)

    i’m pretty sure i used your $liste implode line too, but i should try again as i was in a bit of a hurry. never a good thing with coding! i’m away now for a couple of weeks but will get back to this. thank you so much for your help

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.