PHP Extract Codes From TextArea and Pass to Mysql SELECT IN Query
I have the following data which gets submitted in a text area:
new myProduct('', 'bbc_609'),
new myProduct('', '35857'),
What I am aiming to do is extract the codes from the above and pass just the codes to a Mysql Select IN query.
I can successfully get the codes out no problem with the following:
$text = trim($_POST['newfeatured']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $line) {
$g = preg_match("/new myProduct\('', '(.*?)'\)/i", $line, $match);
$br = $match[1];
echo "<br>";
}
// returns bbc_6093
// returns 5857
However, if I try:
var_dump($br)
... following is returned:
string(7) "bbc_609"
Or if I try to implode and echo, I get nothing back:
$gr = implode(", ", $br);
echo $gr;
//nothing returns
Therefore, given the above, I will not be able to pass the variable to the Mysql statement:
select product.productid, product.name, product.brand, price.code from product inner join price on product.productid=price.productid
WHERE product.productid IN('".implode("','",$br)."')";
//if I pass in $br in this case.
I have also tried without the foreach loop:
if(isset($_POST['newfeatured'])) {
$g = preg_match_all("/new featuredProduct\('', '(.*?)'\)/i",$_POST['newfeatured'], $match);
$g = $match[1];
$codes = implode(", ", $g); //so now we need to be able to pass $codes to the Mysql correctly.
//var_dump($codes); returns the data in the array fine.
}
And when I try to pass $codes to the mysql select IN query, no data is returned?
Any help appreciated.
Cheers
Answer
Solution:
First about the pattern:
\K
to restart the fullstring match.'[^']*'
on the first/empty single quoted component of your input string just in case some text does fill that position.About your query:
?
placeholders are used.call_user_func_array()
is required.bind_result()
to aid in the processing of the resultset.Untested Code: