• Andry

    (@blackstar1991)


    I’m interesting is function JSON_TABLE work for WordPress?
    I’m trying to implement the example described in the documentation with writing json data to the custom table sports_test
    1) Create a table in the WP database

    
    function create_table3()
            {
                global $wpdb;
                require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    
                $table_name = $wpdb->get_blog_prefix() . 'sports_test';
                $charset_collate = $wpdb->get_charset_collate();
    
                if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    
                    $sql = "CREATE TABLE {$table_name}
                    ( id INT NOT NULL AUTO_INCREMENT,
                      xval TEXT NOT NULL default '0',
                      yval TEXT NOT NULL default '0',
                      PRIMARY KEY (id));
                      {$charset_collate}";
                    dbDelta($sql);
    
                    $wpdb->insert(
                        $table_name,
                        array(
                            'id' => 0,
                            'xval' => 'default',
                            'yval' => 'default',
                        )
                    );
                }
            }
            create_table3();
    

    2) I’m trying to write the data as it is written in the documentation example. Something is not working out the syntax.

    
    add_action( 'wp_ajax_clearDB', 'dataStructuring' );
    function dataStructuring(){
    global $wpdb;
        $tb_clear = $wpdb->get_blog_prefix() . 'sports_test';
        $json = '[{\"x\":2,\"y\":\"8\"},{\"x\":\"3\",\"y\":\"7\"},{\"x\":\"4\",\"y\":6}]';
        $wpdb->query("
        UPDATE $tb_clear JSON_TABLE( $json,
                     '$[*]' COLUMNS ( xval TEXT PATH '$.x',
                                      yval TEXT PATH '$.y',
                                      id FOR ORDINALITY
                                      )
                   ) AS  jt1 WHERE id = 1;
        ");
    

    * It works on a simple SQL example. But when I need UPDATE json I got Syntax error How to fix it?

Viewing 3 replies - 1 through 3 (of 3 total)
  • threadi

    (@threadi)

    Sounds like an interesting approach. Could you provide a minimal executable code in which to test the problem?

    Thread Starter Andry

    (@blackstar1991)

    If the code worked, I would not create a question here =)
    https://dbfiddle.uk/btZu0Bwh It’s all what I can to show.

    threadi

    (@threadi)

    No, I mean a code on which you can simply click or call to test the process. In your code above you seem to send an AJAX request. To rebuild all this for testing is a bit complex to be able to test it in the context of voluntary support.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How work JSON_TABLE for WP ?’ is closed to new replies.