Why doesn't the foreach function c PDO?

Hello! A simple code
# Get array of containers
$arr = file(www.'/names.db');

$checkQ = 'SELECT `name` FROM' . $tb['table'] . 'WHERE `name`=?';
$check = $dbh->prepare($checkQ);

foreach($arr as $value) {
 echo '

'. $value.' :: '; $check->execute(['%' . $value . '%']); echo $check->rowCount(); }
the idea is to display a list of strings from a file (in which one name per line) and next to specify the number of matches found in the database.
But it's not working. The name displays correctly, but each name emblazoned 0, and only the latter is 1 (and he really is in the database)
if you remove the foreach and instead of $value to substitute any values - it works.

What could be the problem?

September 26th 19 at 06:42
5 answers
September 26th 19 at 06:44
But why then foreach I che do not understand. Make a request 1 time, the results of the query into an array and print them from the array cycle already as you need.
Then what does the file? PDO is a layer for working with MySQL (and not only)

$stmt = $db->prepare("SELECT * FROM `table` WHERE `name` = :name");
$stmt->bindParam(':name', $name_file);
$stmt->execute();
$arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
$Num = count($arr);


What is $Num and so you have a match. Took this code from a working project.
I mean with it? Despite the fact that this code does not work x\
Did not understand what you write, I need to know how much of a match for the string "name" from a file. - kim_Schmeler77 commented on September 26th 19 at 06:47
I need to know how many of each name, which would then to work with it (if 5 - one, 3 if - else) - kim_Schmeler77 commented on September 26th 19 at 06:50
here is the query number and you will return 5 as 5, 3 so 3 - jaida50 commented on September 26th 19 at 06:53
it returns only an error:
"1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='Array" at line 1' in "
Yes, and think for yourself, you have an array of the file assigned to a variable :name. - kim_Schmeler77 commented on September 26th 19 at 06:56
but, the array. An array in PDO you can not give, there should be a wrapper to write. Read here about working with arrays with PDO stackoverflow.com/questions/920353/can-i-bind-an-a... - jaida50 commented on September 26th 19 at 06:59
are you kidding me? I just need to compare the data array with the base, and I suggest you write a wrapper, and some complicated way. In fact - need to understand why the foreach is not working, instead of writing a huge crutch for a little code. - kim_Schmeler77 commented on September 26th 19 at 07:02
so it's PDO ))) Abstraction over all the database. I also played with him and went on calmly mysqli )) - jaida50 commented on September 26th 19 at 07:05
c mysqli too does not plow, just checked. What is 3.14 zdets - kim_Schmeler77 commented on September 26th 19 at 07:08
You write easier. Author, ubeysya up the wall. The code in the answer inconsistent with reality. - Pierce.Li commented on September 26th 19 at 07:11
the code works cormorant, yourself enter in construction ))) - jaida50 commented on September 26th 19 at 07:14
September 26th 19 at 06:46
Beautiful, beautiful example of what these Q&A sites.
Well, surely a trickle of traffic, consisting of the poor and the LAMER of all kinds, worth it to endure this shame?

The question is, which PDO has the same attitude as myself - to the ballet, and - most importantly - answers, well described by the anecdote about Vovochka "I'm certainly not a Professor..."

Question, the code in which fundamentally will not work, because the author should either remove the cross or panties to put on, and to opredelitsya - fuzzy he Sravana (and add LIKE) or crisp (then remove protsentik).

The answers, in which local intellectuals vying with each other trying to give advice about the PDO, despite the fact that never in my life this library is not seen.

Well, the icing on the cake - it is child's cause of all suffering is artful lines returned by file() by default.
September 26th 19 at 06:48
Translate
$check = $dbh->prepare($checkQ);
inside the loop
From the point of view of efficiency, Yes, it could work, but what you don't warn the person of the queries inside the loop is slooooo! ) - kim_Schmeler77 commented on September 26th 19 at 06:51
Does not help, already tried it. - kim_Schmeler77 commented on September 26th 19 at 06:54
Place the head opposite the brick wall and beat rhythmically to achieve results. - jaida50 commented on September 26th 19 at 06:57
September 26th 19 at 06:50
You have the query wrong, apparently
Correct on
$checkQ = 'SELECT `name` FROM' . $tb['table'] . 'WHERE `name` LIKE ?';


And better even so:
$checkQ = 'SELECT count(`name`) FROM' . $tb['table'] . 'WHERE `name` LIKE ?';

Let the database thinks the string.
September 26th 19 at 06:52
judging by the interest you name LIKE %$value% necessary (well or name = %$value%)?
then the query is likely made crappy and interest should be put into prepare, not execute
Well or as a variant (because of the Array) - $value is not always a string and better to do var_dump(). But probably easier to do a normal bind and not sweating - kim_Schmeler77 commented on September 26th 19 at 06:55
Reveal a little secret: the interest necessary to put it in the execute - kim_Schmeler77 commented on September 26th 19 at 06:58
Yes, it is written in example 6 php.net/manual/ru/pdo.prepared-statements.php and I think it's a bit strange, because on the one hand, the idea is that the query must take a value and make it a template, not Vice versa, and on the other hand - not always the patterns of the form %val%.
somehow, while writing that all is OK stackoverflow.com/questions/4015198/how-to-do-like... - jaida50 commented on September 26th 19 at 07:01
Not to be confused with the template prepared statements, and the search pattern in a LIKE. Then nothing will seem strange. And even better to try to work with PDO in practice. This will help avoid shame in front of others. - kim_Schmeler77 commented on September 26th 19 at 07:04
Yes, I sort of year was in Java, and before that with pdo directly was not the case for different reasons.
So I depths do not care about the conventional shame people asked, and I tried to answer. - jaida50 commented on September 26th 19 at 07:07
Here just trying to answer a topic in which you are neither belmesa not, is a shame. Unconditional. - kim_Schmeler77 commented on September 26th 19 at 07:10
xkcd.com/386 - jaida50 commented on September 26th 19 at 07:13
A great addition to my conclusions about the value, credibility and usefulness of sites like toaster ru. - kim_Schmeler77 commented on September 26th 19 at 07:16
It seems to me that what you think about the toaster - no problem the toaster, right?
And to think that a toaster will suddenly become our stackoverflow (I assume it is something about it?) and when you blame everyone that their answers on the toaster is a complete mess, and people ask simple questions "hamsters and grandmother, the fortune-teller" - rather strange, since one makes it impossible for more :)

At the same time, I somehow agree with you. - Pierce.Li commented on September 26th 19 at 07:19

Find more questions by tags MySQLPHP PDOPHP