espoo
espoo

Reputation: 53

PHP How to read specific words and lines then add to MySQL database

I try to read every word after this word #EXTINF:-1

and the next line from the local file and subsequently add the result to MySQL if it does not exist.

The contents of the file looks like this:

#EXTM3U
#EXTINF:-1,name1
http://www.name1
#EXTINF:-1,name2
http://www.name2
#EXTINF:-1,name3
http://www.name3
#EXTINF:-1,name4
http://www.name4

And my code:

$file = file("file.m3u);
array_shift($file);
$count = count($file);

if($count > 0) {
foreach($file as $row) {
        $pos = strpos($row, ',');
        if($pos !== false){
            $getname[] = substr($row, $pos + 1);
        } else {
            $geturl[] = $row;
} } }

$count = count($getname);
for($i=0; $i < $count; $i++){

$name = $getname[$i];
$url = $geturl[$i];

        if (empty($name)) { exit; };

        if (empty($url)) { exit; }

        $get_user = mysql_query("select * from users where (name = '$name')");
        $show_user = mysql_fetch_array($get_user);
        $userid = $show_user['userid'];

        $get_url = mysql_query("select * from urls where url = '$url'");
        $show_url = mysql_fetch_array($get_url);
        $urlid = $show_url['urlid'];

        if (empty($userid) && empty($urlid)) {

        $add_user = "INSERT INTO users(name)
        VALUES('$name')";
        mysql_query($add_user);
        $userid = mysql_insert_id();

        $add_url = "INSERT INTO urls(userid, url)
        VALUES('$userid', '$url')";
        mysql_query($add_url);
        $urlid = mysql_insert_id();
        }
        }

My code cannot read file correctly, because when I try check the line that I had read from file, it does not work.

The info that I try to read:

name = name1
url = http://www.name1

is for every user.

Upvotes: 1

Views: 203

Answers (1)

cameronjonesweb
cameronjonesweb

Reputation: 2506

This might have something to do with it

$file = file("file.m3u);

It should be

$file = file("file.m3u");

Upvotes: 1

Related Questions