Uploading Data from File to DB

Discussion in 'PHP' started by udkl_12_98, Jan 17, 2008.

  1. #1
    I've been trying to insert data from a "newline" deliminated file using fgets().(Tried other methods also)
    Still after insert , Weird numbers are inserted into the DB .


    The file is based on a linux host , as is the database.

    DB is the default latin_swedish....

    Can anyone Please enlighten me as to what is going on ??
     
    udkl_12_98, Jan 17, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Hello,

    Please can you post the code in question, and some sample data so we can have a crack at it.

    Jay
     
    jayshah, Jan 17, 2008 IP
  3. udkl_12_98

    udkl_12_98 Banned

    Messages:
    307
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here you go Jay ---


    Data written into File (limited amount) :-

    6774715430909970394
    11828365781198180384
    1756115755140636583
    12400341346200954451


    Code ;-


    	   $fp = fopen("filehavingthedata.php","r") or die("error opening file");
    			
    		$contact = fgets($fp);
    		while(!feof($fp)) 
    		 {
    		   $q = "INSERT INTO thetable VALUES ($contact)";
    	       mysql_query($q, $this->connection) or die("Could not Insert $contact");
    		   $contact = fgets($fp);
    		 }
    		
    		fclose($fp);
    PHP:
     
    udkl_12_98, Jan 18, 2008 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    What is inside this txt file?
     
    HuggyStudios, Jan 18, 2008 IP
  5. udkl_12_98

    udkl_12_98 Banned

    Messages:
    307
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    6774715430909970394
    11828365781198180384
    1756115755140636583
    12400341346200954451



    and many numbers are present in the .php file..... Does it matter if it is .php or .txt ?
     
    udkl_12_98, Jan 18, 2008 IP
  6. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #6
    You might need to match your SQL Statement

    
               $q = "INSERT INTO thetable (field) VALUES ($contact)";
    
    PHP:
    Jay
     
    jayshah, Jan 18, 2008 IP
  7. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #7
    1. trim($contact)

    2. is the db field a numeric or string type?
     
    SmallPotatoes, Jan 18, 2008 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    Is your database data type a blob? You need it to be a blob to store data safely.
     
    Kaizoku, Jan 18, 2008 IP
  9. jithosh.js

    jithosh.js Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Use shell_exec command to directly upload data from files to database .

    Example usage

    
    
    $db = mysql_connect("$dbhost","$dbusername","$dbpasswd") or die ("Couldn't connect to server.");
    
    mysql_select_db("$database_name", $connection) or die("Couldn't select database.");
    
    
        $TheFile = "SQL.txt";
        $User = "$dbusername"; 
        $Password = "$dbpasswd";
        $DatabaseName = "$database_name";
    
        $Results = shell_exec( "mysql -u$User -p$Password -hlocalhost $DatabaseName < $TheFile" ) or die ("Error");
    
        echo "Results from MySQL import of $DatabaseName with User $User with file $TheFile:\r\n $Results\r\n";
    
    PHP:
     
    jithosh.js, Jan 18, 2008 IP
  10. udkl_12_98

    udkl_12_98 Banned

    Messages:
    307
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    @ jithosh.js

    Jitosh , I'm on a shared server .... And dont think I'm allowed Shell Access

    @Kaizoku
    It's not Blob . It is 'bigint' with max length 30

    @jayshah
    Tried it earlier ... no avail


    @all
    Thank you for replying . I do appretiate you helping me out .
    The Data inserted into the DB is twice , sometimes thrice or any random number of times .... and it is insert serially (although garbage) like 123123123,124356354,13456456,14456456,2334534543,25567568756
    ,268567567 and on and on ......

    Still havent found the solution , Anyone there who can figure this out ??
     
    udkl_12_98, Jan 18, 2008 IP
  11. udkl_12_98

    udkl_12_98 Banned

    Messages:
    307
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    @SmallPotatoes

    trim() did not help [;(]
     
    udkl_12_98, Jan 18, 2008 IP
  12. udkl_12_98

    udkl_12_98 Banned

    Messages:
    307
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Does it matter if I store the line deliminated data in a .php(server process it as a php script) or in a .txt file ??

    Will try that out now .....
     
    udkl_12_98, Jan 19, 2008 IP
  13. udkl_12_98

    udkl_12_98 Banned

    Messages:
    307
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Sorry Guys .. .my Bad ... The Data(numbers) were being inserted after being being sorted out (automatically , dont know how !!) .. .and being inserted in a serial manner ..... I was looking for the same order as those in the file (not an ordered manner !!) ....

    Thank you all ;)

    <<<< RESOLVED >>>
     
    udkl_12_98, Jan 19, 2008 IP