I have this lines of code in php which reads a file in CSV format with a structure of a mySQL database and display the SQL code needed to create it. The only issue i'm getting here is that i can't print out the primary key variable because it says it's not defined, even though i defined it earlier. Can someone help me explaining why am i getting this error? Here is my code:
<?php
$line = 1;
echo "CREATE TABLE IF NOT EXISTS clientes( <br>";
if (($file = fopen("DB.csv", "r")) !== FALSE) {
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
$str = explode(";", $data[0]);
if($line != 1) {
echo "". $str[0]. " ". $str[1];
if($str[2] == "NO") {
echo " NOT ";
} else {
echo " DEFAULT ";
}
echo " ".$str[4]." ".$str[5].",<br>";
if($str[3] == "PRI") {
$primarykey = $str[0];
}
}
$line++;
}
echo "PRIMARY KEY (" .$primarykey. ") <br>";
echo ") ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
fclose($file);
}
?>