У меня есть 6 столбцов (здание, c1, c2, c3, c4, c5) с переменной длиной, разделенной на символ '&'.
building = "Via Roma 1 ";
1007 * c1 =" C505061CC5BB7245216BB3E7EC7C52C6 & 50B43466133B7D8EC57B6A28727A9EF2 & F0F8659D443B6C36FAE628910DB58F69 ";
1009 * c2 =" 0206EE4050737803AB281ABAA3A229F5 ";
1011 * c3 =" E6D3E9584D7BEB648CC8E54E4A493770 ";
1013 * c4 ="944207DFB4267F08FA1D29296BDEBC86 & DDD4571450E56B7DEA5247B1E88DBD60 ";
c5 =" 6475CC1F4CD44798258A4798CEEA9E6C ";
* 1012,
1012, c *, 1016 * * *. Я попытался сделать это, используя «LIKE» со следующим кодом:
<?php
require('db.php');
if (isset($_POST['id']) && isset($_POST['value'])) {
$output['post_parameters_error_flag'] = false;
}
else
{
$output['post_parameters_error_flag'] = true;
$output['message'] = "Bad POST parameters.";
goto encode;
}
$conn=$dbh->prepare("SELECT value FROM authorized_devices WHERE value = :id");
$conn->execute(array(':id' => $_POST['id']));
if($conn->rowCount() === 1) {
$conn=$dbh->prepare("SELECT building, c1 FROM source WHERE c1 LIKE :value");
$conn->execute(array(':value' => "%{$_POST['value']}%"));
if ($conn->rowCount() > 0) {
$fetch = $conn->fetch(PDO::FETCH_ASSOC);
$position = 0;
if (strpos($fetch['c1'], '&') !== false) {
$splitted_value = explode("&", $fetch['c1']);
for ($i = 0; $i < sizeof($splitted_value); $i++) {
if (strpos($splitted_value[$i], $_POST['value']) !== false) {
$position = $i;
}
}
}
$output['data_error_flag'] = false;
$output['message'] = "Done.";
$output['building'] = $fetch['building'];
$output['type'] = "Value obtained from c1 column";
$output['position'] = $position;
goto encode;
} else {
$output['data_error_flag'] = true;
$output['data_error_id'] = 1;
$output['message'] = "No valid tag found.";
goto encode;
}
$conn=$dbh->prepare("SELECT building, c2 FROM source WHERE c2 LIKE :value");
$conn->execute(array(':value' => "%{$_POST['value']}%"));
if ($conn->rowCount() > 0) {
$fetch = $conn->fetch(PDO::FETCH_ASSOC);
$position = 0;
if (strpos($fetch['c2'], '&') !== false) {
$splitted_value = explode("&", $fetch['c2']);
for ($i = 0; $i < sizeof($splitted_value); $i++) {
if (strpos($splitted_value[$i], $_POST['value']) !== false) {
$position = $i;
}
}
}
$output['data_error_flag'] = false;
$output['message'] = "Done.";
$output['building'] = $fetch['building'];
$output['type'] = "Value obtained from c2 column";
$output['position'] = $position;
goto encode;
} else {
$output['data_error_flag'] = true;
$output['data_error_id'] = 1;
$output['message'] = "No valid tag found.";
goto encode;
}
$conn=$dbh->prepare("SELECT building, c3 FROM source WHERE c3 LIKE :value");
$conn->execute(array(':value' => "%{$_POST['value']}%"));
if ($conn->rowCount() > 0) {
$fetch = $conn->fetch(PDO::FETCH_ASSOC);
$position = 0;
if (strpos($fetch['c3'], '&') !== false) {
$splitted_value = explode("&", $fetch['c3']);
for ($i = 0; $i < sizeof($splitted_value); $i++) {
if (strpos($splitted_value[$i], $_POST['value']) !== false) {
$position = $i;
}
}
}
$output['data_error_flag'] = false;
$output['message'] = "Done.";
$output['building'] = $fetch['building'];
$output['type'] = "Value obtained from c3 column";
$output['position'] = $position;
goto encode;
} else {
$output['data_error_flag'] = true;
$output['data_error_id'] = 1;
$output['message'] = "No valid tag found.";
goto encode;
}
$conn=$dbh->prepare("SELECT building, c4 FROM source WHERE c4 LIKE :value");
$conn->execute(array(':value' => "%{$_POST['value']}%"));
if ($conn->rowCount() > 0) {
$fetch = $conn->fetch(PDO::FETCH_ASSOC);
$position = 0;
if (strpos($fetch['c4'], '&') !== false) {
$splitted_value = explode("&", $fetch['c4']);
for ($i = 0; $i < sizeof($splitted_value); $i++) {
if (strpos($splitted_value[$i], $_POST['value']) !== false) {
$position = $i;
}
}
}
$output['data_error_flag'] = false;
$output['message'] = "Done.";
$output['building'] = $fetch['building'];
$output['type'] = "Value obtained from c4 column";
$output['position'] = $position;
goto encode;
} else {
$output['data_error_flag'] = true;
$output['data_error_id'] = 1;
$output['message'] = "No valid tag found.";
goto encode;
}
$conn=$dbh->prepare("SELECT building, c5 FROM source WHERE c5 LIKE :value");
$conn->execute(array(':value' => "%{$_POST['value']}%"));
if ($conn->rowCount() > 0) {
$fetch = $conn->fetch(PDO::FETCH_ASSOC);
$position = 0;
if (strpos($fetch['c5'], '&') !== false) {
$splitted_value = explode("&", $fetch['c5']);
for ($i = 0; $i < sizeof($splitted_value); $i++) {
if (strpos($splitted_value[$i], $_POST['value']) !== false) {
$position = $i;
}
}
}
$output['data_error_flag'] = false;
$output['message'] = "Done.";
$output['building'] = $fetch['building'];
$output['type'] = "Value obtained from c5 column";
$output['position'] = $position;
goto encode;
} else {
$output['data_error_flag'] = true;
$output['data_error_id'] = 1;
$output['message'] = "No valid tag found.";
goto encode;
}
} else {
$output['data_error_flag'] = true;
$output['data_error_id'] = 2;
$output['message'] = "Unknown device.";
goto encode;
}
encode:
die(json_encode($output));
?>
Но это работает только с некоторыми примерами полей. Любые допустимые альтернативы "LIKE"?
Вывод должен быть:
{
"post_parameters_error_flag": false,
"data_error_flag": false,
"message": "Done.",
"building": "Via Roma 1",
"type": "obtained from c1",
"position": 1
}
С $ _POST ['value'] => "50B43466133B7D8EC57B6A28727A9EF2"