I have an exercise where i've been given a link where there's data stored on a .csv file.
Now:
- My php page (using fgetcsv) already parses for the csv fields from the link so i have an identical page but in html
- I already created a database with the same identical structure so i can import my data
I couldn't find anything useful around.. my questions are:
- How can i proceed to import the data from the output i have on my php page?
- Is what i'm trying to do actually a good way to solve this case?
Thanks in advance
@RiggsFolly
The code i use that parses and displays the data i need is:
UPDATED CODE:
$servername = "localhost";
$username = "user";
$password = "psw";
$conn = mysqli_connect("localhost","user" ,"psw","dbname");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "you did it";
}
$row = 1;
if (($handle = fopen("givenurl.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
$sql = "INSERT INTO tablename($columns) VALUES ($data[$c])";
}
}
fclose($handle);
}
I'm actually wondering how to make the matching between the arrays i got parsing the csv and the columns i have on the database
I'll give an example of the arrays i get:
[0] name; surname; email;
[1] john; cleon; [email protected];
(The first array matches the database structure)
p.s. maybe i got something wrong and i should declare the columns with the
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…