I have a fasta file input.fa which looks like this:
>KJH325_Org_name_strain
ANNTTHWQLPMCVREEDFSC
>IJA254.1_Org_name
HITYYPQLKSSCMART
>ASDL658_Org_name_str
TTILPQWYERSAASMNCFGHDKLCC
and so on.
I want to enter these short sequences into a Mysql table using PHP such that the KJH325
(i.e., the ID after the first underscore '_') goes into ID column and the rest Org_name or Org_name_strain
goes into the orgname column and the short sequence in the next line goes into sequence column.
I have tried this:
<?php
$servername = "localhost";
$dbname = "shortseqdb";
// Create connection
$conn = mysqli_connect($servername, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully\n\n";
$fasta_file = 'input.fa';
$header='';
$id = '';
$org_name = '';
$lines = file($fasta_file) or die("Unable to open file!");
foreach ($lines as $line) {
$header = strpos($line, '>'); <---- UPDATED
if ($header!==false) {
$key = trim(substr($line, 0, $header));
$value = trim(substr($line, $header+1));
list($id, $org_name) = explode("_",$key);
$sql = "INSERT INTO `seqs` (`ID`, `org_name`, `sequence`) VALUES ($id, $org_name, $value)";
}
}
if (mysqli_query($conn, $sql)) {
echo "Values inserted successfully in the table seqs\n";
} else {
echo "Error inserting values: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
It connects successfully but I am getting the following error:
Connected successfully
Notice: Undefined offset: 1 in C:\xampp\htdocs\test.php on line 26
Error inserting values: No database selected
Could anybody help me where is this script wrong? Thanks!
I found out that the problem was in username and password. I looked into the privileges on XAMPP in the same database and it turned out I had to specify the username and password as well.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "shortseqdb";
// Create connection
$conn = mysqli_connect($servername, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully\n\n";
// select a db
mysqli_select_db($conn,"shortseqdb") or die("Could not select the specified database");
And used mysqli_db_connect($connect,dbname)
to select the specified database. Then it worked perfectly.
Thanks for your comments.