LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PHP - Help With Reading Lines In A File (http://www.linuxquestions.org/questions/programming-9/php-help-with-reading-lines-in-a-file-440711/)

windisch 05-01-2006 08:39 PM

PHP - Help With Reading Lines In A File
 
I have a text file that has a list of filenames that are in a queue to be played. I would like to create a script that will read each line (each song's file path) and then search my MYSQL database to display a nice list, instead of the file path. Any help would be great!

Here is the code for the MYSQL search:

PHP Code:

mysql_connect('localhost',$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `musicmetadata` WHERE `filename` LIKE '%$song_queue%'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();


$i=0;
while (
$i $num) {

$artist=mysql_result($result,$i,"artist");
$album=mysql_result($result,$i,"album");
$title=mysql_result($result,$i,"title");
$filename=mysql_result($result,$i,"filename");

echo 
"<b>Artist: <P2>$artist</P2> Title: <P2><a target='Log' href='request.php?request_id=$intid'>$title</a></P2><br>Album: <P2>$album</P2></b><hr>";

$i++;
}

?> 


95se 05-01-2006 09:21 PM

I'm not sure what your question is, but you can just use fgets to read lines from a file handle.
Code:

$fh = fopen('playlist.txt', 'r');
while(!feof($fh)) {
  $line = fgets($fh);
  ....
}

Use mysql_fetch_assoc or something too, it's a heck of a lot easier.
Code:

while($row = mysql_fetch_assoc($result)) {
  echo "<b>Artist: <P2>${row['artist']}</P2> Title: <P2><a target='Log' href='request.php?request_id=${row['intid']}'>${row['title']}</a></P2><br>Album: <P2>${row['album']}</P2></b><hr>";
}


windisch 05-02-2006 08:32 PM

Thanks, let me see what I can do with that. I'm trying to come up with a script to read a line and then search my mySQL database.

windisch 05-03-2006 08:00 AM

I get this error in my error_log file:

Code:

[client 68.73.129.207] PHP Parse error:  parse error, unexpected $ in /var/www/html/shoutcast/request_queue2.php on line 43
[client 68.73.129.207] PHP Parse error:  parse error, unexpected $ in /var/www/html/shoutcast/request_queue2.php on line 43
[client 68.73.129.207] PHP Parse error:  parse error, unexpected $ in /var/www/html/shoutcast/request_queue2.php on line 43
[client 68.73.129.207] PHP Parse error:  parse error, unexpected $ in /var/www/html/shoutcast/request_queue2.php on line 43

The code is only 42 lines, so I'm not sure whats wrong. Here is the code as it is now, somethings not exactly right.
PHP Code:

<?php
$fh 
fopen('MythMusic.alw''r');
while(!
feof($fh)) {
  
$line fgets($fh);
$username="mythtv";
$password="mythtv";
$database="mythconverg";

mysql_connect('localhost',$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `musicmetadata` WHERE `filename` LIKE '%$line%'";
$result=mysql_query($query);

#$num=mysql_numrows($result);

mysql_close();

#$i=0;
#while ($i < $num) {

#$artist=mysql_result($result,$i,"artist");
#$album=mysql_result($result,$i,"album");
#$title=mysql_result($result,$i,"title");
#$filename=mysql_result($result,$i,"filename");

#echo "<b>Artist: <P2>$artist</P2> Title: <P2><a target='Log' href='request.php?request_id=$intid'>$title</a></P2><br>Album: <P2>$album</P2></b><hr>";

while($row mysql_fetch_assoc($result)) {
  echo 
"<b>Artist: <P2>${row['artist']}</P2> Title: <P2><a target='Log' href='request.php?request_id=${row['intid']}'>${row['title']}</a></P2><br>Album: <P2>${row['album']}</P2></b><hr>";
#}

#$i++;
}
?>


windisch 05-03-2006 08:18 AM

Nevermind, I commented out something I shouldn't have. This it great!

One last question and it should be done. File names in the queue look like this:

Code:

/video3/MythMusic/Radio Queue/Toad the Wet Sprocket/Coil/Whatever I Fear.mp3
I need to remove everything before Radio Queue, so it looks like this:

Code:

Radio Queue/Toad the Wet Sprocket/Coil/Whatever I Fear.mp3
I'm not good with reg expression, can anyone help with that?

graemef 05-03-2006 08:29 AM

You want to use:

strpos -- Find position of first occurrence of a string
substr -- Return part of a string

giving you something like:

$newPath = substr($oldPath, strpos($oldPath, "Radio Queue"));

windisch 05-03-2006 12:48 PM

That seems to do the trick for the file name, thanks!

My sql search doesn't seem to be returning anything now. I'm going to try a few things and see what I get.


All times are GMT -5. The time now is 08:34 PM.