这应该可以,不确定 mysql 部分,我只是通过w3schools.com查找的
$con = mysqli_connect('host','username','password','dbname');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
// Slice the data by lines first.
$linesOfData = explode('\n',$myText); // becomes an array of data
// $linesOfData = array_filter($linesOfData); // uncomment this if you feel like the data will come with an empty line, removes any empty values in the array.
// loop through the sliced data
foreach($linesOfData as $lineOfData) {
$arrayOfValues = explode(',',$lineOfData);
// loop through the array of values
foreach($arrayOfValues as $value) {
/* condition_statement ? value_if_yes : value_if_false
This is equivalent to
if(!empty($value[0]){$title=$value[0]}else{$title=null}
to check if the array has that index, to avoid error
*/
$title = !isset($value[0]) ? trim($value[0]) : null;
$name = !isset($value[1]) ? trim($value[1]) : null;
$url = !isset($value[2]) ? trim($value[2]) : null;
$number = !isset($value[3]) ? trim($value[3]) : null;
// insert to database, not recommended for commercial projects, this is vulnerable to sql injections, you should learn about prepared statements.
mysqli_query($con,"INSERT INTO tablename (title, name, url, number) VALUES ('$title','$name','$url','$number')");
}
}
// close the connection immediately after using it
mysqli_close($con);
}