How work JSON_TABLE for WP ?
-
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 databasefunction 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)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How work JSON_TABLE for WP ?’ is closed to new replies.