gpt4 book ai didi

php - 读取excel文件并将其存储到mysql数据库

转载 作者:行者123 更新时间:2023-12-04 21:25:04 25 4
gpt4 key购买 nike

我有以下代码,它在执行时出错。

<?php
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('exceltestsheet.xlsx');

$conn = mysql_connect("localhost","root","");
mysql_select_db("exceltest",$conn);
for ($x = 2; $x <= count($data->sheets[0]["cells"]); $x++) {
// $sno = $data->sheets[0]["cells"][$x][1];
$name = $data->sheets[0]["cells"][$x][1];
$extension = $data->sheets[0]["cells"][$x][2];
$email = $data->sheets[0]["cells"][$x][3];
$sql = "INSERT INTO mytable (name,extension,email)
VALUES ('$name',$extension,'$email')";
echo $sql."\n";
mysql_query($sql);
}
?>

错误是: Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\www\xltodb\Excel\reader.php on line 262它还说文件名 exceltestsheet.xlsx 不可读。

reader.php 的代码是:
 function Spreadsheet_Excel_Reader()
{
$this->_ole =& new OLERead();
$this->setUTFEncoder('iconv');
}

这是显示错误的行。

最佳答案

看一下这个

    <?php
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('siteb_ceccwo.xls');

$con=mysqli_connect("localhost","root","","exceltest");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Create table
$sql="CREATE TABLE siteb_ceccwo(

quote_number VARCHAR(100),
line_no VARCHAR(100),
item_no VARCHAR(100),
name VARCHAR(100),
unit VARCHAR(100),
rm VARCHAR(100),
cwo_rm VARCHAR(100))";

// Execute query
if (mysqli_query($con,$sql))
{
echo "Table siteb_ceccwo created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}

for($x = 2; $x <= count($data->sheets[0]["cells"]); $x++)
{
//$sno = $data->sheets[0]["cells"][$x][1];
$quote_number = $data->sheets[0]["cells"][$x][1];
$line_no = $data->sheets[0]["cells"][$x][2];
$item_no = $data->sheets[0]["cells"][$x][3];
$name = $data->sheets[0]["cells"][$x][4];
$unit = $data->sheets[0]["cells"][$x][5];
$rm = $data->sheets[0]["cells"][$x][6];
$cwo_rm = $data->sheets[0]["cells"][$x][7];

$res = mysqli_query($con,"INSERT INTO siteb_ceccwo
(quote_number, line_no, item_no,name,unit,rm,cwo_rm) VALUES ('$quote_number','$line_no','$item_no','$name','$unit','$rm','$cwo_rm')");
mysqli_close($res);
}
?>

关于php - 读取excel文件并将其存储到mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com