Jump to content

Search Database mutliple times for diffrent things


dopey

Recommended Posts

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;  
        } 

Link to comment
Share on other sites

;) 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>"

Link to comment
Share on other sites

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>"

 

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.