Jump to content

Insert/Replace help


Twitch

Recommended Posts

I have a text file with rows that look like below:

123245,Name of Case

263746,Another Name of Case

 

and so on.....

 

My goal is to have a table called case_numbers update each night from this text file.  I'm not worried about the automated part yet, I can't even get the data to insert yet...ha ha

 

I basically want the data in the table replaced with the data in the text file as the text file will have new case numbers added often.

 

After looking and looking on the net, I have this code:

<?php 
mysql_connect ("localhost","username","password"); 
mysql_select_db ("database_name"); 

// Path to the data file 
$path="test.txt"; 

// Get the site of the file 
$filesize=filesize($path); 

// Open file, read its data and close it 
$filenum=fopen($path,"r"); 
$arrlen = count($filenum); 
fclose($filenum); 

// Listing the array inside a loop, adding it to the database and echo about it. 
for($i=0; $i<$arrlen ; $i++){
$bits=explode(",",$filenum[$i]);

$sql_insert = mysql_query("REPLACE INTO case_numbers (casenumber,casename) VALUES ('$bits[0]','$bits[1]'");

$result = mysql_query ($sql_insert); 
// reports an error if insert fails. 
if (!$result){ echo mysql_error();} 
}else{
// All done, let's go home! 
echo "All rows imported successfully!"; 
}
?> 

Of course I put my actual connection settings for the connections.

 

When I run it I don't get an error, but the data in the table isn't replaced.  Nothing happens.

 

Any idea where I'm going wrong.

 

Thanks in advance,

Twitch

 

Made an edit cause I noticed my explode statement was missing the ","  still doesn't work..ha ha

Link to comment
Share on other sites

As jl5501 pointed out, you are not actually reading the file....try

mysql_connect ("localhost","username","password"); 
mysql_select_db ("database_name"); 
// Path to the data file 
$path="test.txt"; 

// Get the site of the file 
$filesize=filesize($path); 
// Open file, read its data and close it 
$filenum=fopen($path,"r"); 

$contents = fread($filenum, filesize($filesize));
fclose($filenum); 

$bits=explode(",",$contents);
$arrlen = count($bits);

// Listing the array inside a loop, adding it to the database and echo about it. 
for($i=0; $i<$arrlen ; $i++){

$sql_insert = mysql_query("REPLACE INTO case_numbers (casenumber,casename) VALUES ('$bits[0]','$bits[1]'");

$result = mysql_query ($sql_insert); 
// reports an error if insert fails. 
if (!$result)echo mysql_error(); 
}
// All done, let's go home! 
echo "All rows imported successfully!"; 

Link to comment
Share on other sites

Thanks so much for the replies guys.  This site never disappoints.  I tried the code above and I get "Query was emptyAll rows imported successfully!"  I tried to echo $result; but get nothing.  Evidently it's not grabbing the data from the file.

 

I know I must be missing something simple.  Maybe when my coffee kicks in it will be apparent...haha

 

 

Link to comment
Share on other sites

Well, my friends I started from scratch and have an almost working script.

<?php 
// Make a MySQL Connection
mysql_connect("connection", "username", "pword") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

$nextline = ''; 
$fp = fopen('test.txt','r'); 
while (!feof($fp)) { 
$mystuff = array(); 
$nextline = fgets($fp); 
$mystuff = split(',',$nextline); 
$casenumber = mysql_real_escape_string(trim($mystuff[0])); 
$casename = mysql_real_escape_string(trim($mystuff[1])); 
mysql_query("REPLACE INTO case_numbers 
(casenumber, casename) VALUES('$casenumber', '$casename' ) ") 
or die(mysql_error());  
} 
echo '<p>done!'; 
?>

 

The text file will be updated with new case numbers and names.  When I added a new entry to the bottom of the text file and ran the above script, the new entry was added to the database.  However, if I delete the last entry, the above script does not delete the entry from the database.  There are only the two fields in the table, casenumber and casename with casenumber set to unique.  I'm sure it's just a little tweak to the query to see if one is missing and if so delete it.  Any help would be much appreciated.  Learned a lot so far doing this script.

 

Thanks in advance.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.