I have a .xls file with a couple of fields, and I need to put the information into a database and pull it into a web page. What is the easiest way to do this? I am concerned about getting the .xls file into a mySQL database, how will I do this?
I know I need to use the following code to pull the information to the php web page:
PHP Code:
<?php // Make a MySQL Connection mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("database") or die(mysql_error());
// Retrieve all the data from the "example" table $result = mysql_query("SELECT * FROM example") or die(mysql_error());
// store the record of the "example" table into $row $row = mysql_fetch_array( $result ); // Print out the contents of the entry
I would save it as a CSV file, then use explode to separate into individual columns (hoping that you don't have a comma in the text you are trying to insert, if so, you need to choose another delimiter).
Here is some code to handle the insertion:
Code:
<?php
$fp = fopen("yourfile.csv", "r");
while (($data = fgetcsv($fp, 1000, ",")) !== FALSE) {
mysql_query("INSERT INTO table VALUES ('" . $data[0] . "', '" . $data[1] . "', '" . $data[2] . "', '" . $data[3] . "')"); // put in the appropriate fields here. $data[#] represents the column number in the csv file.
}
fclose ($fp);
?>
You will need to have a MySQL table setup with the columns you want to have. Let me know if you need help with this.
I can't figure out how to get this to work using this code to get the email address:
PHP Code:
<?PHP
// Connecting, selecting database
$link = mysql_connect('localhost', 'Username', 'Password') or die('Could not connect: ' . mysql_error());
mysql_select_db('Database') or die('Could not select database');
// Performing SQL query $query = 'SELECT * FROM TABLE'; $result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML echo "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) {