LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-01-2006, 08:39 PM   #1
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Rep: Reputation: 30
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++;
}

?> 

Last edited by windisch; 05-01-2006 at 08:43 PM.
 
Old 05-01-2006, 09:21 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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>";
}
 
Old 05-02-2006, 08:32 PM   #3
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
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.
 
Old 05-03-2006, 08:00 AM   #4
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
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++;
}
?>
 
Old 05-03-2006, 08:18 AM   #5
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
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?
 
Old 05-03-2006, 08:29 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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"));
 
Old 05-03-2006, 12:48 PM   #7
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
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.
 
  


Reply

Tags
php


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 12:36 AM
Reading random lines in c++? twirl Programming 7 10-30-2005 10:11 AM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM
reading mails using php shenil Linux - Newbie 4 05-28-2005 04:36 PM
PHP not reading PHP_AUTH_USER slackwarefan Linux - Software 0 10-11-2004 06:41 PM


All times are GMT -5. The time now is 05:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration