Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

How can i use the output of a php page to import data on a database?

I have an exercise where i've been given a link where there's data stored on a .csv file.

Now:

  1. My php page (using fgetcsv) already parses for the csv fields from the link so i have an identical page but in html
  2. 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:

  1. How can i proceed to import the data from the output i have on my php page?
  2. 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're already parsing the the CSV using fgetcsv to display it in HTML then you just need to write the CSV data (from the array that fgetcsv gives you) to your database table (many examples around if you just need help writing the array to mysql).

I'd suggest if you're going to store to a local database, you would be better then retrieving the data from your database to display on your page - rather than relying on the external linked CSV file.

If the CSV data changes regularly you'll need to think about how to handle those changes in terms of either updating your local database (assuming there's a unique ID in the CSV you can use to identify the right row in your table to update), or drop the existing data and replace at intervals. This could be done to a schedule (cron job perhaps) or perhaps a manual import script you run when you're aware the data has been changed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...