Hello to all,
I am having problems getting an uploaded file to place a portion of its contents into a mySQL database. By portion I mean that there are only certain lines from the input file that I want to extract. My problem isn’t with connecting to the database or uploading the file, it is with the code that operates on the file. Here is the code that I have:
[CODE]<?php
// Eliminate Error Notice
error_reporting(E_ALL & ~E_NOTICE) ;
// This will get an input file from an html page and deposit it
// into a mySQL database
// Gregory Koenig
// Last modified – 10/9/08
// Test if file was uploaded
if(! $_FILES[‘dataFile’][‘tmp_name’])
{
echo “<html><body><h1>No file uploaded</h1></body></html>” ;
exit ;
}
// Test connection to mySQL
// Don’t forget to change the password to ‘******’ !!!!!
$link = mysql_connect( ‘localhost’, ‘*******’, ‘*******’ )
or die( ‘Could not connect to mySQL:’.mysql_error() ) ;
echo “Successful connection !! nn” ;
// Test connectionto database ‘gkoenig’
mysql_select_db( ‘gkoenig’)
or die(‘Could not connect to database!’) ;
echo “Successful selection of database !! nn” ;
// This is where we input the file into the database
// using mySQL commands
$fh = fopen( $_FILES[‘dataFile’][‘tmp_name’], ‘r’) ;
// Parse through Swiss-Prot file and extract the seq_id (Accession),
// seq_type (PRT,DNA, RNA), and seq_data
while($text = fgets($fh))
{
if(feof($fh))
{
exit ;
}
// Grab the seq_type from first line
if(strstr($text, “ID”))
{
$token = strtok($text, “t”) ;
$pre_seq_type = $token[3] ;
// Knock off semi-colon
$seq_id = $token[0].$token[1].$token[2] ;
}
// Grab the seq_id from the second line
if(strstr($text, “AC”))
{
$token2 = strtok($text, “t”) ;
$seq_id = $token2[1] ;
}
// Jump to sequences and collect
if(strstr($text, “SQ”))
{
fgets($fh) ; // Jump to the next line
// Collect sequence
while( ($c = fgetc($fh)) != “/”)
{
$seq_data[] = $c ;
}
}
// Put data into Database
$query = “INSERT into sequences VALUES (‘”.$seq_id.”‘, ‘”.$seq_type.”‘, ‘”.$seq_data.”‘) ” ;
$result = mysql_query($query)
or die(‘Data insertion failed:’.mysql_error() ) ;
// Empty sequence array so that it can accept new data
while ($seq_data)
{
array_pop($seq_data) ;
}
}
?>
<html>
<head>
<title>Sequences in Database</title>
</head>
<body>
<h1>Sequences in Database</h1>
<?php
/*
// Show data that was input into ‘sequences’ Database
$query2 = ‘SELECT * from sequences’ ;
$result2 = mysql_query($query2)
or die(‘Query failed:’.mysql_error() ) ;
echo “<table border=1> n” ;
while( $row = mysql_fetch_assoc($result2) )
{
echo “t<tr>t” ;
foreach ($row as $col)
{
echo “tt<td>$col</td>n” ;
}
echo “t</tr>n” ;
}
echo “</table>n” ;
*/
?>
</body>
</html>
The file that it will work on looks like this (I will only include the relevant portions:
ID 1433F_HUMAN Reviewed; 246 AA.
AC Q04917;
DT 01-OCT-1993, integrated into UniProtKB/Swiss-Prot.
DT 23-JAN-2007, sequence version 4.
.
.
. ” this represents a bunch of info that will be skipped
.
.
FT TURN 213 215
FT HELIX 216 234
SQ SEQUENCE 246 AA; 28219 MW; D70FBC100C45D6E5 CRC64;
MGDREQLLQR ARLAEQAERY DDMASAMKAV TELNEPLSNE DRNLLSVAYK NVVGARRSSW
RVISSIEQKT MADGNEKKLE KVKAYREKIE KELETVCNDV LSLLDKFLIK NCNDFQYESK
VFYLKMKGDY YRYLAEVASG EKKNSVVEAS EAAYKEAFEI SKEQMQPTHP IRLGLALNFS
VFYYEIQNAP EQACLLAKQA FDDAIAELDT LNEDSYKDST LIMQLLRDNL TLWTSDQQDE
EAGEGN
//
When I run the script it was not placing what I wanted into the database.
Any help would be great. If I am not that clear in the post and I will post any other info I can.
Thanks