sending files to the database (PHP &MySQL)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Miss.Apache
Forum Newbie
Posts: 10
Joined: Sun Dec 14, 2003 11:49 am

sending files to the database (PHP &MySQL)

Post by Miss.Apache »

Hello every body :)

i have a probelm with sending a file to the database (MySQL)

i implement a code (with PHP) that will call a C compiler then recieved the outpt from the compiler. it saves this output in a file. now i want to send the file to the database!

i don't know how?

would you please give a help? :oops:
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Does the file contain text that you want to send into records in the db?
Miss.Apache
Forum Newbie
Posts: 10
Joined: Sun Dec 14, 2003 11:49 am

Post by Miss.Apache »

Yes
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Then you'll have to look into the file functions for php and then look into the LOAD INTO SQL queries.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

you could also do something like a parser that will read the file line by line, and insert into a table.

if the log file itself contains the HEADER's on the first line or so, you can grab those and generate a table dynamically based on whatever information you have.

then, after creating the table, you can just read line by line. however, there is the problem with dealing with delimitations. who is the file formatted? is it comma delimited? space delimited? hyphen delimited? Etc...

in any case, here is a pretty nifty way to do this sorta thing assuming that you have headers, and then the actual information.. all space delimited : (note, this is untested, but should give you a general idea)

Code: Select all

mysql_connect('localhost', 'username', 'password') or die(mysql_error()); 
	mysql_select_db('my_db') or die(mysql_error()); 
	$file_contents = file('log.txt');
	foreach ($file_contents as $num => $line_data) 
	{
		$line_no = $num + 1; 
	//if it is line 4, then we are at the header, so parse it and post it.
   		if ($line_no == 4) 
		{ 
			$parsed_data = preg_split("/[\s,]+/", $line_data);
			$parsed_data[0] = 'Header 1';
			$parsed_data[1] = 'Header 2';
			$parsed_data[2] = 'Header 3';
			$sql = "CREATE TABLE loginfo(".$parsed_data[0]." varchar(18), ".$parsed_data[1]." varchar(18),".$parsed_data[2]." date";
			mysql_query($sql) or die(mysql_error());
		}
	// if we are at line 5 or any other line to EOF, then we are seeing data, so update to table
		elseif ($line_no > 4) 
		{ 
			// Read column and update.
			$info_data = preg_split("/[\s]+/", $line_data);
			$qry="insert into loginfo (".$parsed_data[0].",".$parsed_data[1].",".$parsed_data[2].") values ('".$info_data[0]."','".$info_data[1]."','".$info_data[2]."')";
			mysql_query($qry) or die(mysql_error());
   		} 
	}

in any case, a good resource would be :

http://www.php.net/file
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

That's what I said :D
Post Reply