I have Table by name "centers" column by name "teams" :
Schema (MySQL v5.7)
DROP TABLE IF EXISTS `Centers`;
CREATE TABLE IF NOT EXISTS `Centers` (
`id` int(11) NOT NULL ,
`name` VARCHAR(255) NOT NULL ,
`teams` JSON NOT NULL
);
INSERT INTO `Centers`
(`id`, `name`, `teams`)
VALUES
('1','Texas','{"teams0":{"name":"Alex","training":"worker","agenda":"https:\/\/example.com\/booking\/alex","terminland_days":"0"},"teams1":{"name":"thomas","training":"worker","agenda":"https:\/\/example.com\/booking\/thomas","terminland_days":"0"}}'),
('2','California','{"teams0":{"name":"Dean","training":"worker","agenda":"https:\/\/example.com\/booking\/dean","terminland_days":"0"},"teams1":{"name":"Rose","training":"worker","agenda":"https:\/\/example.com\/booking\/rose","terminland_days":"0"}}');
Query #1
SELECT VERSION();
VERSION() |
---|
5.7.22 |
Query #2
SELECT `id`, `Name`, `teams`
FROM `Centers`;
id | Name | teams |
---|---|---|
1 | Texas | {"teams0": {"name": "Alex", "agenda": "https://example.com/booking/alex", "training": "worker", "terminland_days": "0"}, "teams1": {"name": "thomas", "agenda": "https://example.com/booking/thomas", "training": "worker", "terminland_days": "0"}} |
2 | California | {"teams0": {"name": "Dean", "agenda": "https://example.com/booking/dean", "training": "worker", "terminland_days": "0"}, "teams1": {"name": "Rose", "agenda": "https://example.com/booking/rose", "training": "worker", "terminland_days": "0"}} |
also I have a Function for get "agenda" value then return "terminland_days" :
function get_single_agents_info($url)
{
$arr = explode('/', $url);
$center_slug = $arr[count($arr) - 2];
$agent_slug = $arr[count($arr) - 1];
$data = array("key" => "11111", "request" => "get_agents_info","office_seo_url" =>$center_slug,"agent_seo_url"=>$agent_slug);
$json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/booking');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'Atlantomed API');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$answer = curl_exec($ch);
curl_close($ch);
$json_answer = json_decode($answer);
if ($json_answer->status == 'OK') {
foreach ($json_answer->agents as $agent) {
foreach ($agent->offices as $office) {
return $office;
/* print_r ($office);
result =====> [next_appointments] => Array ( [0] => stdClass Object ( [remaining_days] => 3 )) */
}
}
}
return null;
}
so in final, I must get "Agenda url" from database and send it to Function, then updating teams
column by function return object.
this my wrong php code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sql";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'SELECT * FROM Centers WHERE teams LIKE "%example.com%"'; /* this wrong */
/* i need select `agenda` in json column */
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$url = $row['agenda'];
$agents_data = get_single_agents_info($url);
$next_appointments = !empty($agents_data->next_appointments) ? $agents_data->next_appointments[0] : [];
if (!empty($next_appointments)) {
$terminland_date = $next_appointments->date;
$terminland_days = $next_appointments->remaining_days;
$updatesql = "UPDATE `Centers` SET `terminland_days` = '" . $terminland_days . "' WHERE `agenda` = '" . $row['agenda'] . "'";
$conn->query($updatesql);
}
}
}
Question: how can update JSON Column teams
in mysql ?
Update:
php code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sql";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'SELECT *,
CAST(object_name AS CHAR) object_name,
CAST(JSON_EXTRACT(teams, CONCAT("$.", object_name, ".name")) AS CHAR) name,
CAST(JSON_EXTRACT(teams, CONCAT("$.", object_name, ".agenda")) AS CHAR) agenda,
CAST(JSON_EXTRACT(teams, CONCAT("$.", object_name, ".terminland_days")) AS CHAR) terminland_days
FROM Centers
JOIN ( SELECT id, JSON_EXTRACT(JSON_KEYS(teams), CONCAT("$[", n, "]")) object_name
FROM Centers
JOIN ( SELECT 0 n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) numbers
ON n < JSON_LENGTH(JSON_KEYS(teams))) paths USING (id)';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$url = $row['agenda'];
$agents_data = get_single_agents_info($url);
$next_appointments = !empty($agents_data->next_appointments) ? $agents_data->next_appointments[0] : [];
if (!empty($next_appointments)) {
$terminland_date = $next_appointments->date;
$terminland_days = $next_appointments->remaining_days;
/* here */
$updatesql = "UPDATE Centers
SET teams = JSON_SET(teams, CONCAT('$.', @object_name, '.terminland_days'), @new_value)
WHERE agenda = @id;";
$conn->query($updatesql);
}
}
}