MySQL query is
SELECT CompanyName, RegistrationNumber, VatCode
FROM 18_6_TransactionPartners
WHERE (CompanyName = ? OR RegistrationNumber = ? OR VatCode = ?)
?
replace with $data_show_existing_records
. But initially $data_show_existing_records
is in form of string (user input).
At first decided to create such $data_show_existing_records = $data_show_existing_records. $data_a[$i]. ','. $data_b[$i]. ','. $data_d[$i]. ',';
(this is created within foreach, because $i
may be only 0, but may also be 100)
And convert to array in such way
$data_show_existing_records = substr($data_show_existing_records, 0, -1);
$data_show_existing_records = explode(",", $data_show_existing_records);
But for example for CompanyName
user may input My, company
or leave some input empty.
In such case I will get error SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
. Because there would be more $data
in array than ?
.
Then decided replace ,
with |
(because suppose |
is less used than ,
).
But this seems not good solution. What would be better (what to use as string/user input separator)? May be just need to escape ,
(but how)? Please, advice
Update
Found solution for empty input: $data_show_existing_records = $data_show_existing_records. "$data_a[$i]". ','. "$data_b[$i]". ','. "$data_d[$i]". ',';
But what to do with ,
?
Next update
Create initial query(shortened code) and data for the query
foreach ($num_row_1 as $i => $row) {//$num_row_1 is number of rows in user input
$query_to_show = $query_to_show. 'WHERE (CompanyName = ? OR RegistrationNumber = ? OR VatCode = ?) ';
$data_show_existing_records = $data_show_existing_records. "$data_a[$i]". '"|"'. "$data_b[$i]". '"|"'. "$data_d[$i]". '"|"';
}
Get for example:
$query_to_show =
WHERE (CompanyName = ? OR RegistrationNumber = ? OR VatCode = ?) OR (CompanyName = ? OR RegistrationNumber = ? OR VatCode = ?)
and
$data_show_existing_records =
first name"|"123"|"112233"|"second name"|"456"|"445566"|"
Then convert to array
$data_show_existing_records = substr($data_show_existing_records, 0, -1);
$data_show_existing_records = explode('"|"', $data_show_existing_records);
And then final query
$query_show_existing_records = "
SELECT CompanyName, RegistrationNumber, VatCode
FROM 18_6_TransactionPartners
$query_to_show
";
And prepare/execute
$sql_show_existing_records = $db->prepare($query_show_existing_records);
$sql_show_existing_records->execute($data_show_existing_records);
$data_show_existing_records1 = $sql_show_existing_records->fetchAll(PDO::FETCH_ASSOC);
Solution
Changed preparation for query and data to following:
$flag = 0;//Set as WHERE
$data_show_existing_records = array();
foreach ($num_row_1 as $i => $row) {
if($flag == 0) {
$query_to_show = $query_to_show. 'WHERE (CompanyName = ? OR RegistrationNumber = ? OR VatCode = ?) ';
$flag = 1;
}
else {
$query_to_show = $query_to_show. 'OR (CompanyName = ? OR RegistrationNumber = ? OR VatCode = ?) ';
}
$data_show_existing_records[] = $data_a[$i];
$data_show_existing_records[] = $data_b[$i];
$data_show_existing_records[] = $data_d[$i];
}