I am unable to get my text file data.txt to read correctly into my database.
This is what my text file looks like

9889483782,6068378394,6069437364... and so on for like 50 numbers

I have to get each of the comma seperated values to show up in the database, id like them to be placed in their own respective row in my table numdata where the values will be inserted in the field, Numb.

My problem is that im not getting any errors? but my code produces no results. What do i do to fix this?
Also please try to elaborate and use code examples, i learn better by seeing it work as well as understaing how it works. Thank You.

Another quick note, the data in the txt file has nothing else in it, no spaces or anything just comma seperated vlaues.

<?php

    require ('dbconnect.php'); // connect to database

    // initial database stuff

        $file = "data.txt";
        $file_handle = fopen($file, "r");


        fclose($file_handle);

            while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE) 
            {  

                $i = 1;
                foreach($file as $row)
                {
                    $data = explode(',',$row);
                    $query .= $i < $count ? ',':'';
                    $i++;
                }
                $query="INSERT into Numbers(Numb) values('$data')";
                mysql_query("TRUNCATE TABLE Numbers") or die("MySQL Error: " . mysql_error()); //Delete the existing rows
                mysql_query($query) or die(mysql_error());  
            }  




echo "Done!";
?>

I think you are closing the file too early.

Your code should be:

<?php
require ('dbconnect.php'); // connect to database
// initial database stuff
$file = "data.txt";
$file_handle = fopen($file, "r");

$file = "data.txt";
$file_handle = fopen($file, "r");
mysql_query("TRUNCATE TABLE Numbers") or die("MySQL Error: " . mysql_error()); //Delete the existing rows
while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE)
{
    foreach($data as $row)
    {
        $query="INSERT into Numbers(Numb) values($row)";
        mysql_query($query) or die(mysql_error()); 
    }
}
fclose($file_handle);

echo "DONE!";
?>

TYSM now im actually getting some errors, however its saying now that my table doesnt exist?
here is the error.

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload/upload.php on line 14

Notice: Array to string conversion in /opt/lampp/htdocs/upload/upload.php on line 20
MySQL Error: Table 'Numbers.Numbers' doesn't exist

I dont understand? i mean the only line i changed was just that one and even though i know the table does exist my code says it doesnt?

sigh if it aint one thing its anohter :/

See the code I posted. Compare my foreach loop with yours. $data is an array, so you can't use explode on it. Plus your want to loop for each element of the array so the correct loop is what I wrote.
Also your Table does exist, but you are using the wrong database name. Post a screenshot of your table in the databse. And also post your dbconnect.php file(without the passwords of course ;) ).

well i checked the database issue but i didnt get far :( here is the dbconnect.php

<?php
    $host = "*****";        // the host your database server is on, probably localhost
    $username = "*****";        // Database Username, if you are doing this locally and haven't set one it will probably be "root"
    $password = ******;            // Database Password, if you are doing this locally and haven't set one it will probably be NULL 
    $database = "Numbers";    // The name of the database you want to use.

    // Try to connect to mysql
    mysql_connect($host, $username, $password) or die ("Could not connect to database");  

    // Try to select the database 
    mysql_select_db($database) or die ("Could not select database");    
?>

and here is the screen shot of my table, at least i hope this is the part needed, and also ty so so much for all this, ive been working on this for a while but with little to show for it.

No, I wanted to see the name of your database and the table. I think you are confusing between the database name and the tablename.

i think this is it.

So your table name is numbdata and not numbers, so the query should be:
$query="INSERT into numbdata(Numb) values($row)";
and the truncate query: mysql_query("TRUNCATE TABLE numbdata") or die("MySQL Error: " . mysql_error());
Update the query in my code and you are all set.
By the way, are you sure you want to truncate the table data??

i mean i dont really need to since there is only comma seperated data in my file but someone i know told me it was good practice to clean my data? not sure but i tried your solution and its still giving me this error
MySQL Error: Table 'Numbers.Numbers' doesn't exist

Paste your full code now.
You can truncate if you don't need that data now. I was just confirming if you really need to.

here is the upload.php

?php

    require ('dbconnect.php'); // connect to database

    // initial database stuff

        $file = "data.txt";
        $file_handle = fopen($file, "r");

        mysql_query("TRUNCATE TABLE Numbers") or die("MySQL Error: " . mysql_error()); //Delete the existing rows
        while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE)
        {
            foreach($data as $row)
            {
                $query="INSERT into numbdata(Numb) values($row)";
                mysql_query($query) or die(mysql_error()); 
            }
        }

fclose($file_handle);


echo "Done!";
/*


    // Form to upload a new file, make sure you get the "enctype" or it won't work
    echo "
        <form  enctype='multipart/form-data' name='fileupload' action='upload.php' method='POST'>

            <input type='file' name='uploadedfile'> 
            <input type='submit' name='upload' value='Upload File'>

        </form>
    ";
*/
?>

and here is the dbconnect.php

<?php
    $host = "";        // the host your database server is on, probably localhost
    $username = "";        // Database Username, if you are doing this locally and haven't set one it will probably be "root"
    $password = ;            // Database Password, if you are doing this locally and haven't set one it will probably be NULL 
    $database = "Numbers";    // The name of the database you want to use.

    // Try to connect to mysql
    mysql_connect($host, $username, $password) or die ("Could not connect to database");  

    // Try to select the database 
    mysql_select_db($database) or die ("Could not select database");    
?>
commented: Check if you did completely what was asked before posting here. You updated only one line and not the other. +0

See, you didn't update the truncate line(line 10) see my previous post.
It should be: mysql_query("TRUNCATE TABLE numbdata") or die("MySQL Error: " . mysql_error());

OMG it works :D now there is one more little part id like help with if you would be so kind. The part of my file that is in html that is an upload form, how would i go about combining that with my current setup where the file would be uploaded by a user and automatically read into the data base? like they would upload from where ever and the result would all the info in the file would be read into the database

and also seriously thank you so much, i am forever grateful :)

The upload form:

 <form enctype='multipart/form-data' name='fileupload' action='upload.php' method='POST'>
<input type='file' name='uploadedfile'>
<input type='submit' name='upload' value='Upload File'>
</form>

Should be in a separate file by any name. You need to have an input of type file in the form as well. The uploaded file should be processed and inserted into db like above in upload.php. To read more about file upload refer this.
And don't forget to mark the question solved and up-vote and comment the answer.
If you have any doubt in the upload thing, make another post for that.

ok now here is something interesting, im getting the strangest error with my completed program, i admit my test file data.txt was for testing purposes but now im using the main file which is MUCH MUCH larger and its giving me this

Warning: fopen(606.txt): failed to open stream: Permission denied in /opt/lampp/htdocs/upload.php on line 8

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/upload.php on line 11

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload.php on line 13

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/upload.php on line 11

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload.php on line 13

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/upload.php on line 11

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload.php on line 13

note the 11 and 13 line errors repeat forever, any idea why this is happening when it was JUST working??

do this in the htdocs directory and paste its output here: ls -l 606.txt
You may not have read permission for the file.

nvm it was a linux issue, its working fine now but its slow as christmas lol ty u so much for all the help

i ended up changing the permission of the folder with these commands

"ps aux | grep httpd"
this command was to see what folders had owner ship, in this case it was daemon

"sudo chown daemon /opt/lampp/htdocs"
this was to grant access to the folder

"sudo chmod -R 0755 /opt/lampp/htdocs"
this one allowed me to write to the directory

all were needed to fix my issue but now its working fine :D thank you for all your wisdom, it was all very much appreciated.

"sudo chmod -R 0755 /opt/lampp/htdocs"
this one allowed me to write to the directory

all were needed to fix my issue but now its working fine :D thank you for all your wisdom, it was all very much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.