-1
<?php

    $con = mysqli_connect("localhost","root","","addressbook");


    $file="localhost/IMDBAPI/-title.ratings.tsv";
    
    $row = 1;
    if (($handle = fopen($file, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000000, "\t")) !== FALSE) { 
            $num = count($data); 
            $row++;
        
            $id=$data[0];
            $name=$data[1];
            $address=$data[2];
            $phone=$data[3];

            $sql="INSERT INTO adress (First_Name, Surname, Address) VALUES ('".$name."','".$address."','".$phone."')";
            mysqli_query($con, "SELECT * FROM adress");
        }
    }


    fclose($file);
?>

I have a TSV file named title.ratings.tsv and I am trying to insert values from that file into a MySQL table named adress (yes it is spelled incorrectly) into the columns First_Name, Surname, Address but I am getting the error ,

( ! ) Warning: fopen(localhost/IMDBAPI/-title.ratings.tsv): failed to open stream: No such file or directory in C:\wamp64\www\tsv.php on line 9

And,

( ! ) Warning: fclose() expects parameter 1 to be resource, string given in C:\wamp64\www\tsv.php on line 25

GoncaloRM
  • 7
  • 2
  • It's looking for a folder in the working directory called localhost. You most likely don't have one. `fopen` works with the system directory structure, not the webserver paths, unless it starts with `scheme://`. See https://www.php.net/manual/en/function.fopen.php – aynber Dec 14 '21 at 20:23
  • @aynber Ok, I have changed it to the directory of the actual file on my PC, but now I am getting this error, and repeating A LOT OF TIMES (lagging my PC) I think its due to the size of the actual TSV file . ( ! ) Notice: Undefined offset: 3 in C:\wamp64\www\tsv.php on line 17 What would this refer to? – GoncaloRM Dec 14 '21 at 20:29
  • It means `$data[3]` doesn't exist, so that row in the file doesn't have 4 values. – aynber Dec 14 '21 at 20:33
  • Also, `fclose($file);` should be `fclose($handle);` – aynber Dec 14 '21 at 20:36
  • @aynber I have now fixed both issues, and when I run the wamp code online it loads for a while and I even added a ```echo "success"; ``` which ends up appearing at the end, but my SQL table is still empty :/ – GoncaloRM Dec 14 '21 at 20:42
  • Because while you're creating an insert string, you never pass it to your database. Your mysqli_query is doing a select instead. – aynber Dec 14 '21 at 20:44
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Dec 14 '21 at 21:09
  • @aynber so how would I insert it into the database? – GoncaloRM Dec 14 '21 at 21:27
  • Change out the select statement with your insert statement, of course – aynber Dec 15 '21 at 12:43
  • @aynber I changed it to ```mysqli_query($con, $sql);``` and my table remains empty – GoncaloRM Dec 15 '21 at 13:00
  • I would note that you're open for SQL injection issues and quoting issues. Using [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [bind_param](http://php.net/manual/en/mysqli-stmt.bind-param.php) will take care of that, especially if there's any single quotes in your values. Otherwise, check for [mysqli errors](http://php.net/manual/en/mysqli.error.php) to find out why it's failing. – aynber Dec 15 '21 at 13:05
  • @aynber ahh yes that makes the error more clear. Now I am recieving MAAAANY errors saying "Incorrect integer value: 'averageRating' for column 'tconst' at row 1Incorrect integer value: 'tt0000001' for column 'numVotes' at row 1Incorrect integer value: 'tt0000002' for column 'numVotes' at row 1... etc) Thoughts on this? – GoncaloRM Dec 15 '21 at 19:40
  • Well, `tt0000001` and `tt0000002` obviously aren't integers, which `averageRating` and `numVotes` are expecting. You may need to double-check your values and what you're sending to the database. – aynber Dec 15 '21 at 19:46

1 Answers1

0
  1. The name of File is indiferent. Is the name of file
  • "-title.ratings.tsv" or
  • "title.ratings.tsv" (without minus)
  1. Do you have set permissions?

chmod 777 -title.ratings.tsv

Kap Gallo
  • 1
  • 2
  • Yea the problem was mainly the name of the file, and I fixed that but I am running into another problem – GoncaloRM Dec 14 '21 at 21:28
  • When you post an answer, it should be an answer. This is not an answer. If you want to ask the person asking a question for additional information, you should post it as a comment. – Palo Dec 14 '21 at 21:38
  • @Palo, he doesn't have enough reputation to leave a comment. – user1597430 Dec 15 '21 at 00:23
  • @user1597430 good point. That is not an acceptable excuse though. – Palo Dec 16 '21 at 08:32