【问题标题】:Convert a mysql for loop to mysqli for loop将 mysql for 循环转换为 mysqli for 循环
【发布时间】:2015-12-03 08:12:28
【问题描述】:

我在互联网上找到了一段代码,它完全符合我的要求。该代码是关于使用PHP mysql将mysql数据下载为excel表格格式。

问题是我需要将 mysql 转换为 mysqli 函数,它似乎不起作用。 for 循环需要彻底改变。有什么建议吗?

$conn = mysqli_connect("localhost","root","","dvarsam2"); //server,username,password,db
mysqli_query($conn,'SET NAMES utf8');

$setCounter = 0;

$setExcelName = "download_excal_file";

$setSql = "SELECT * FROM wp_applications";

$setRec = mysqli_query($conn,$setSql);

$setCounter = mysql_num_fields($setRec);

for ($i = 0; $i < $setCounter; $i++) {
    $setMainHeader .= mysql_field_name($setRec, $i)."\t";
}

while($rec = mysql_fetch_row($setRec))  {
  $rowLine = '';
  foreach($rec as $value)       {
    if(!isset($value) || $value == "")  {
      $value = "\t";
    }  else  {
//It escape all the special charactor, quotes from the data.
      $value = strip_tags(str_replace('"', '""', $value));
      $value = '"' . $value . '"' . "\t";
    }
    $rowLine .= $value;
  }
  $setData .= trim($rowLine)."\n";
}
  $setData = str_replace("\r", "", $setData);

if ($setData == "") {
  $setData = "\nno matching records found\n";
}

$setCounter = mysql_num_fields($setRec);



//This Header is used to make data download instead of display the data
 header("Content-type: application/octet-stream");

header("Content-Disposition: attachment;      filename=".$setExcelName."_Reoprt.xls");

header("Pragma: no-cache");
header("Expires: 0");

//It will print all the Table row as Excel file row with selected column name as header.
echo ucwords($setMainHeader)."\n".$setData."\n";

【问题讨论】:

标签: php mysql mysqli


【解决方案1】:

对于 MySQLi:

<?php

$conn = mysqli_connect("localhost","root","","dvarsam2"); //server,username,password,db
mysqli_query($conn,'SET NAMES utf8');

$setExcelName = "download_excal_file";

//This Header is used to make data download instead of display the data
header("Content-type: application/octet-stream");

header("Content-Disposition: attachment;      filename=".$setExcelName."_Reoprt.xls");

header("Pragma: no-cache");
header("Expires: 0");

$result = mysqli_query( $conn, "SELECT * FROM wp_applications" );

$i = 0;

while( $row = $result->fetch_assoc() )
{
  if( $i == 0 )
  {
    // Print field names
    foreach( $row as $key => $value )
    {
      echo $key."\t";
    }

    echo "\n";
  }

  // Print data
  foreach( $row as $key => $value )
  {
    $value = strip_tags(str_replace('"', '""', trim($value)));
    echo '"' . str_replace("\r", "", $value ) . '"' . "\t";
  }

  echo "\n";

  $i++;
}

if( $result->num_rows == 0 ) echo "no matching records found\n";

【讨论】:

  • 它工作正常。唯一的问题是 excel 中的字符以奇怪的编码显示。
【解决方案2】:

您也可以只修复您的 SELECT 语句以格式化 Excel 格式的输出:

SELECT * FROM mytable
INTO OUTFILE '/mytable.csv'
FIELDS ESCAPED BY '""'
TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';

请参阅here 了解更多信息。您提供的代码来自接受的答案,但我更喜欢这个解决方案。

【讨论】:

  • 我也喜欢它。运行速度可能快 5 倍
  • 请注意,这通常不适用于共享主机环境,因为INTO OUTFILE 选项通常在该环境中被禁用。
猜你喜欢
  • 2015-12-20
  • 2012-10-13
  • 2021-03-12
  • 2018-02-22
  • 2017-04-26
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多