Reputation: 35
I am breaking my neck over this problem. So I have a text file "$file" that I am reading line by line with the first while-loop. This is working; when I echo the "$usertext" in there, it prints nicely all the lines that are in the file.
So I want to use the $usertext as a condition for my DB query and then loop through all the rows of the DB that meet that condition. Somehow only the last line of $usertext gets in the while-loop (it is the only one that gets printed when I echo $usertext in the second while-loop).
Why do not all lines of my text file go through the second while-loop?
if ($file) {
while (($line = fgets($file)) !== false) {
$usertext = $line;
echo $usertext."\n";
$result = $db->query("SELECT user, artist, artist_mbid, track, track_mbid, COUNT(*) as playcount FROM users WHERE user='$usertext' GROUP BY artist, track, user");
while($row=$result->fetchArray()){
echo $usertext."\n";
//I also used $row["user"]; does not make any difference
}
}
}
Upvotes: 0
Views: 215
Reputation: 3658
More likely to be data not found than an SQL error as an SQL error would stop the processing.
The output of fgets()
, AFAIK, includes the line break. This would mean that the contents of $usertext
might contain a line break on every record except the last, so only the last call to $result->fetchArray()
would give an output record and so enter the while
loop.
My advice is to try changing this line:
$usertext = $line;
To
$usertext = trim($line);
which would remove any unwanted line breaks / white space.
Upvotes: 1
Reputation: 16111
$result->fetchArray()
might return a falsey value for some of your result sets.
Make sure $usertext
is getting inserted into the query correctly and does not break it, as well as making sure there actually are rows to be returned.
Upvotes: 0