Tag: mysqli

PHPEXCEL:如何从excel文件读取每一行并插入数据库

我想上传一个excel文件,逐行读取并将数据插入到数据库中。 我怎么可以这样做? 这是我迄今为止所尝试的: for ($row = 1; $row <= $highestRow; $row++) { // Read a row of data into an array $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); // This line works as $sheet->rangeToArray('A1:E1') that is selecting all the cells in that row from cell A to highest […]

错误:在使用PHP将mysqli查询结果导出到excel中

我想MySQL的查询结果到Excel中,因此我写了下面的PHP脚本: <?php $conn = mysqli_connect('localhost','root','xyz','abc'); $select = "SELECT * FROM table_name"; $export = mysqli_query( $conn,$select) or die ( "Sql error : " . mysqli_error($conn) ); $fields = mysqli_num_rows ( $export ); for ( $i = 0; $i < $fields; $i++ ) { //But this line giving error continuously $header .= mysqli_fetch_field_direct($export,$i) . "\t"; } while( […]

MySQL到Excel字符集问题

我有一个数据库设置,接受用户注册和他们的详细信息等。我正在寻找出口数据库使用PHP的Excel文件。 我遇到的问题是,一些参赛者已经input了诸如土耳其语之类的外国字符,这些字符已经被错误地写入了数据库 – 就我所知,字符集可能在第一次被错误地设置时制作。 我已经使我的代码导出数据库到Excel(下面),但我不能让Excel文档正确显示,无论我如何尝试编码数据 <?php require_once('../php/db.php'); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=Download.xls"); header("Pragma: no-cache"); header("Expires: 0"); $query = "SELECT * FROM users"; $result = mysqli_query($link, $query); if($result) { $count = mysqli_num_rows($result); for($i=0; $i<$count; $i++) { $field = mysqli_fetch_field($result); $header .= $field->name."\t"; while($row = mysqli_fetch_row($result)) { $line = ''; foreach($row as $value) { if((!isset($value)) OR ($value […]

将MySQLi查询导出到XLS

我创build了这个小脚本,用于在一些教程的帮助下将MySQLi查询导出到.xls。 本教程使用MySQL,但我使用MySQLi。 该文件成功导出并可读,但将数据放在第二行。 这是我的代码: <?php include 'connectToDB.php'; $table = "users"; $filename = "users_export"; $sql = "Select * from $table"; $result = mysqli_query($conn,$sql) or die("Couldn't execute query:<br>" . mysqli_error(). "<br>" . mysqli_errno()); $file_ending = "xls"; header("Content-Type: application/xls"); header("Content-Disposition: attachment; filename=$filename.xls"); header("Pragma: no-cache"); header("Expires: 0"); $sep = "\t"; $names = mysqli_fetch_fields($result) ; foreach($names as $name){ print ($name->name […]

将MySQL数据库以CSV格式导出到PHP中的桌面

我一直在寻找方法来启用我的数据库网站的导出function。 我的目标是构build一个非常简单的PHP代码来做到这一点,它只需要将我的一个MySQL表的内容导出到用户的桌面,或给用户一个可点击的链接,以便他可以下载导出的数据库。 该数据库必须在Excel / CSV(因为我将使用此function导入/导出数据库备份function以及)。 我看到fputcsv()和其他旧的方法,如mysql(而不是mysqli)代码,但都失败了,有些创build了一个CSV文件在服务器的目录,而其他方法简单地给出了一个错误 explode()期望参数2是string,给定的对象 要么 fputcsv()期望参数2是数组,null是给定的 我觉得这应该是简单的,但是用于导出数据库function的最佳方法或代码是什么? 编辑: 在结合和testing一系列选项后,我能够解决这个问题。 我在这里分享,所以那些会遇到我的困境的人不会像我一样难过:( 当用户点击EXPORTbutton时,Chrome浏览器会自动下载一个包含字段名称的数据库副本的CSV文件。 <?php include('/connectme.php'); $thequery = "SELECT * FROM members"; $result = mysqli_query($conn,$thequery); $file = fopen("php://output","w"); header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="exported.' . date("Ymd") . '.csv"'); header('Pragma: no-cache'); header('Expires: 0'); $emptyarr = array(); $fieldinf = mysqli_fetch_fields($result); foreach ($fieldinf as $valu) { array_push($emptyarr, $valu->name); } […]

PHP – 另存为文件(excel)

我有一个Excel的工作下载文件,但首先我想把一个下载窗口popup。现在,它会自动生成而不保存文件窗口。 我怎样才能做到这一点。 请帮帮我。 <html> <head> <script language="javascript"> function download() { window.location='report.xls'; } </script> </head> <body alink="#00FF66" link="#00CC00"> <h1 align="center"><a href="javascript:void(0);" onClick="download();">Download Excel Report</a></h1> <?php require_once("db.php"); require_once("excelwriter.class.php"); $excel=new ExcelWriter("report.xls"); if($excel==false) echo $excel->error; $myArr=array("Model","Code","Desc","Stock Requested","Stock Received", "Diff", "On hand"); $excel->writeLine($myArr); $sql="SELECT allinvty3.*, barcode.* , sample.*, count(barcode.itemcode) as icount from barcode INNER JOIN sample on barcode.itemcode = sample.in_code […]

使用PHPExcel将SQL数据从数据库导出到Excel

我正尝试使用PHPExcel将数据从我的数据库导出到Excel。 我用这个问题的代码。 编辑mysql到mysqli并将扩展名更改为xlsx格式。 但是,当我想打开文件,我得到以下错误(我翻译成英文,所以对不起我英文不好,如果不对) 无法打开文件交stream29-05-2017.xlsx,因为文件格式或文件扩展名无效。 检查文件是否没有损坏,文件扩展名是否与文件格式匹配。 这是我复制和更改的代码: <?php require 'database.php'; require '../Classes/PHPExcel.php'; if (isset($_POST['exporteer'])) { $a = $_POST['organisatie_naam']; $b = $_POST['begin_datum']; $c = $_POST['eind_datum']; $datum = date("dmY"); $objPHPExcel = new PHPExcel(); $query = "SELECT * FROM register WHERE organisatie_naam = '$a' AND (registratie_aansluit_datum BETWEEN '$b' AND '$c'"; $result = mysqli_query($db, $query); $objPHPExcel = new PHPExcel(); […]

PHP / MySQLi excel导出没有列标题

我有一些我用来导出到Excel使用MySQL的代码。 我将语法切换到MySQLi,它的工作原理除了不在Excel中打印列标题。 我是MySQLi的新手,请原谅我。 这里是代码(我会尽量省略不必要的代码): <?php include("../include/database.php"); global $ts; $ts = date('mdY-His'); session_start(); $where = $_SESSION['where']; $sql = "SELECT * FROM `main_table` WHERE " . $where . ""; $result = mysqli_query($dbc, $sql) or die(mysqli_error()); header("Content-Type: application/xls"); header("Content-Disposition: attachment; filename=importdetails-".$ts.".xls"); header("Pragma: no-cache"); header("Expires: 0"); header("Content-Transfer-Encoding: binary "); // here is the formatting for Excel $sep = "\t"; […]

从PHP导出数据从SQL到Excel

我试图导出sql到csv与PHP,这是我设法做的。 它没有两件事情运作良好:1.在csv输出的sql数据之前,我得到当前页面的HTML代码出于某种原因。 2.如何更改表格标题行? 想要重命名csv中的表头列。 $result = $db->query("SELECT * FROM user_history WHERE member_id = '$user_id'"); $num_fields = $result->field_count; $headers = array(); $headers[] = "Number"; for($i = 0; $i < $num_fields; $i++ ) { $Obj = $result->fetch_field_direct($i); $headers[] = $Obj->name."\t"; } $current_date = date("y/m/d"); $filename = "MyFileName" . $current_date . ".csv"; $fp = fopen('php://output', 'w'); if ($fp […]