You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将MySQL查询结果从屏幕发送至打印机?附PHP查询代码

如何将MySQL查询结果发送至打印机

嘿,我来帮你搞定把MySQL查询结果从屏幕输出到打印机的需求!咱们一步步来:

首先,我注意到你代码里用了$newdate和$newdate1但没定义,还有$today也没用到,先把这些小问题补上,再添加打印相关的功能。

完整实现代码

<?php
$host = "你的数据库地址";
$user = "你的数据库用户名";
$password = "你的数据库密码";
$database = "你的数据库名称";

// 连接数据库
$cxn = mysqli_connect($host,$user,$password,$database) or die ("Could not connect to database");

// 定义日期范围(这里示例为过去7天到未来7天,你可以根据实际需求调整)
$today = date("Y/m/d");
$newdate = date("Y/m/d", strtotime("-7 days"));
$newdate1 = date("Y/m/d", strtotime("+7 days"));

// 安全的SQL查询(避免SQL注入风险)
$sql = "SELECT * FROM `lafont` WHERE weekending> ? and weekending < ? order by weekending";
$stmt = mysqli_prepare($cxn, $sql);
mysqli_stmt_bind_param($stmt, "ss", $newdate, $newdate1);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$num = mysqli_num_rows($result);
?>

<!DOCTYPE html>
<html>
<head>
    <title>打印查询结果</title>
    <style>
        /* 专门的打印样式,确保打印内容排版整齐 */
        @media print {
            body * {
                visibility: hidden;
            }
            .print-content, .print-content * {
                visibility: visible;
            }
            .print-content {
                position: absolute;
                left: 0;
                top: 0;
            }
            table {
                border-collapse: collapse;
                width: 100%;
                margin-top: 20px;
            }
            th, td {
                border: 1px solid #333;
                padding: 8px;
                text-align: left;
            }
            th {
                background-color: #f5f5f5;
            }
        }
    </style>
</head>
<body>
    <!-- 点击就能触发打印的按钮 -->
    <button onclick="window.print()">打印查询结果</button>

    <!-- 专门用来打印的内容区域 -->
    <div class="print-content">
        <h2>LaFont 数据表查询结果</h2>
        <p>查询日期范围: <?php echo $newdate; ?> 至 <?php echo $newdate1; ?></p>
        
        <?php if($num > 0): ?>
            <table>
                <thead>
                    <tr>
                        <!-- 这里要替换成你lafont表实际的字段名,比如id、name、weekending等 -->
                        <th>ID</th>
                        <th>名称</th>
                        <th>周末日期</th>
                        <!-- 继续添加其他字段的表头 -->
                    </tr>
                </thead>
                <tbody>
                    <?php while($row = mysqli_fetch_assoc($result)): ?>
                        <tr>
                            <td><?php echo $row['id']; ?></td>
                            <td><?php echo $row['name']; ?></td>
                            <td><?php echo $row['weekending']; ?></td>
                            <!-- 对应添加其他字段的内容 -->
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        <?php else: ?>
            <p>没有找到符合条件的数据哦</p>
        <?php endif; ?>
    </div>

    <?php 
    // 关闭数据库连接
    mysqli_stmt_close($stmt);
    mysqli_close($cxn); 
    ?>
</body>
</html>

几个关键要点说明

  • 打印触发:页面上的按钮用window.print()调用浏览器自带的打印功能,点击后就能选择你的打印机进行输出。
  • 打印样式优化:通过@media print CSS规则,只让.print-content里的内容显示在打印预览中,隐藏了打印按钮,避免不必要的元素被打印出来。
  • SQL注入防护:我把你原来的SQL拼接改成了预处理语句,这样能避免恶意的SQL注入攻击,让你的代码更安全——直接拼接变量到SQL里是很危险的,一定要注意!
  • 字段适配:记得把代码里的表头和字段内容替换成你lafont表实际的字段名,不然可能会显示错误。

内容的提问来源于stack exchange,提问作者Ken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:27:00