dopey Posted January 16, 2011 Share Posted January 16, 2011 Hello, im looking for some help if anyone can be of help... i want to read from a file each line of that file, for each line search the database for that line found in the file and return what comes back, but i want it todo this for the whole file which could be up to 500 diffrent lines. what i have so far doesnt work ive tried a number of things including foreach() some of this could i got some somewhere else they said it worked for them. $lines = file("/var/www/htdocs/mp32/playlist.lst"); require('dbconfig1.php');//connect to db script while($n < count($lines)) { //get number of lines in file or array $recipeQuery = mysqli_query($conn1, "SELECT * FROM mp_id3_tags WHERE filename LIKE = '$lines[$n]'"); //query database for each one while($recipeQuery1 = mysqli_fetch_array($recipeQuery)) { print "{$recipeQuery1['artist']}"; } $n = $n + 1; } Quote Link to comment https://forums.phpfreaks.com/topic/224598-search-database-mutliple-times-for-diffrent-things/ Share on other sites More sharing options...
requinix Posted January 16, 2011 Share Posted January 16, 2011 Every item in the array returned by file() has the newlines attached. You need to get rid of it. Quote Link to comment https://forums.phpfreaks.com/topic/224598-search-database-mutliple-times-for-diffrent-things/#findComment-1160200 Share on other sites More sharing options...
ignace Posted January 16, 2011 Share Posted January 16, 2011 Every item in the array returned by file() has the newlines attached. You need to get rid of it. Or add the FILE_IGNORE_NEW_LINES flag. Quote Link to comment https://forums.phpfreaks.com/topic/224598-search-database-mutliple-times-for-diffrent-things/#findComment-1160295 Share on other sites More sharing options...
dopey Posted January 16, 2011 Author Share Posted January 16, 2011 Thanks for the help i added the no new line tag tot he file() and also have it strip the file of any colons or commas if i didnt do this i would get a error back from MYSQL i tried to use mysqli_real_escape_string but i couldnt get it to work right... but heres what i got and it works if anyone ever need a code like this. This code loads kinda slow im sure has todo with how many query's are being sent to the mysql server. anyone have a fix for that please post $lines = file("file", FILE_IGNORE_NEW_LINES); require('dbconfig1.php'); $bad_symbols = array("'",","); //set bad symbols $linesgood = str_replace($bad_symbols,"",$lines); // remove bad symbols i was using mysql real escape but couldnt get it to work. print "<cenetr><table><tr><th>whaetver colum1</th><th>whatever colum2</th></tr>"; while($n < count($linesgood)) { //get number of lines in file or array $recipeQuery = mysqli_query($conn1, "SELECT * FROM mp_id3_tags WHERE filename LIKE '$linesgood[$n]'"); //query database for each one while($recipeQuery1 = mysqli_fetch_array($recipeQuery)) { print "<tr><td>{$recipeQuery1['row1']}</td><td>{$recipeQuery1['row2']}</td></tr>"; } $n = $n + 1; } print "</table></center>" Quote Link to comment https://forums.phpfreaks.com/topic/224598-search-database-mutliple-times-for-diffrent-things/#findComment-1160346 Share on other sites More sharing options...
jcbones Posted January 16, 2011 Share Posted January 16, 2011 Have you tried running a single query, like: $lines = file("file", FILE_IGNORE_NEW_LINES); require('dbconfig1.php'); $bad_symbols = array("'",","); //set bad symbols $linesgood = str_replace($bad_symbols,"",$lines); // remove bad symbols i was using mysql real escape but couldnt get it to work. print "<cenetr><table><tr><th>whaetver colum1</th><th>whatever colum2</th></tr>"; $recipeQuery = mysqli_query($conn1, "SELECT * FROM mp_id3_tags WHERE filename IN ('" . implode('\',\'',$linesgood) . "')"); //query database for each one while($recipeQuery1 = mysqli_fetch_array($recipeQuery)) { print "<tr><td>{$recipeQuery1['row1']}</td><td>{$recipeQuery1['row2']}</td></tr>"; } print "</table></center>" Quote Link to comment https://forums.phpfreaks.com/topic/224598-search-database-mutliple-times-for-diffrent-things/#findComment-1160358 Share on other sites More sharing options...
dopey Posted January 16, 2011 Author Share Posted January 16, 2011 I has trieed your code out,. it works fast 2 things it doesnt do the other does print $n for each result and list all in order of being read from the file file line1 line2 line3 when the result comes back mysql puts them in a order it knows which could be ASC or DESC but then you need a tag or id in the mysql database todo that.. here how the mysql returns it with the bit you gave me line2 line3 line1 here what i have as of now require('dbconfig1.php'); // connect to database $bad_symbols = array("'",","); // set bad symbols $linesgood = str_replace($bad_symbols,"",$lines); // remove bad symbols i was using mysql real escape but couldnt get it to work. print "<center><table border=1 frame=void cellpadding=1 cellspacing=0 align=center rules=all><tr><th>Song Position</th><th>Artist</th><th>Title</th><th>Genre</th></tr>"; while($n < count($linesgood)) { //get number of lines in file or array $query = mysqli_query($conn1, "SELECT * FROM mp_id3_tags WHERE filename LIKE '$linesgood[$n]'"); //query database for each one while($row = mysqli_fetch_array($query)){ print "<tr><td>$n</td><td>{$row['artist']}</td><td>{$row['title']}</td><td>{$row['genre']}</td></tr>"; // print whats returned from mysql and print the number it is in the file read from } $n = $n + 1; } print "</table></center>"; Quote Link to comment https://forums.phpfreaks.com/topic/224598-search-database-mutliple-times-for-diffrent-things/#findComment-1160407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.