You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

PHP+MySQL执行后显示空白页 新手求助(附代码)

解决页面空白问题(给PHP+MySQL新手的分步指南)

先别慌,咱们一步步拆解问题,你的代码里藏着几个新手容易踩的坑,我帮你逐个排查修复:


1. 先换废弃的mysql_*函数(核心问题!)

你用的mysql_query()mysql_fetch_array()这些函数早在PHP 5.5就被废弃了,PHP7+直接移除了这些函数,这大概率是页面空白的主要原因!赶紧换成mysqli_*(更适合新手的扩展),同时补上你缺失的数据库连接代码

// 先加数据库连接,替换成你自己的数据库信息
$host = 'localhost';
$dbname = '你的数据库名称';
$username = '数据库用户名';
$password = '数据库密码';

// 创建连接并检查是否成功
$conn = mysqli_connect($host, $username, $password, $dbname);
if (!$conn) {
    die("数据库连不上:" . mysqli_connect_error());
}

// 改写查询代码
$query = "SELECT * FROM tbladmin"; 
$rs = mysqli_query($conn, $query);

// 新手必加:检查查询是否出错,不然出错了看不到任何提示
if (!$rs) {
    die("查询失败:" . mysqli_error($conn));
}

$html =""; 
while($row = mysqli_fetch_array($rs)) { 
    // 注意:别用<这种转义符,直接写正常HTML标签,否则浏览器会把标签当文本显示!
    $html .= '<tr> 
        <td>'.$row['adm_id'].'</td> 
        <td>'.$row['adm_name'].'</td> 
        <td>'.$row['adm_email'].'</td> 
        <td>'.$row['adm_password'].'</td> 
        <td>'.$row['adm_gender'].'</td> 
        <td> 
            <img src="Uimages/'.$row['adm_photo'].'" height="90%" width="90%" alt="No Photo"> 
        </td> 
    </tr>'; 
} 

2. 修复HTML语法错误

你原来的HTML有两个明显的小错误,也会导致页面显示异常:

  • <img>标签没闭合,少了结尾的>
  • 多写了一个冗余的</td>
  • 把HTML标签用&lt;转义了,浏览器会把<tr>这类标签当成普通文本,不会解析成表格行

修正后的HTML部分:

<table class="listing" cellpadding="0" cellspacing="0"> 
    <tr> 
        <th bgcolor="#999999" scope="col"><strong>ID</strong></th> 
        <th bgcolor="#999999" scope="col"><strong>Name</strong></th> 
        <th bgcolor="#999999" scope="col"><strong>E-mail</strong></th> 
        <th bgcolor="#999999" scope="col"><strong>Password</strong></th> 
        <th bgcolor="#999999" scope="col"><strong>Gender</strong></th> 
        <th bgcolor="#999999" scope="col"><strong>Photo </strong></th> 
    </tr> 
    <?php echo $html; ?> 
</table>

3. 最后排查其他可能的空白原因

如果改完还是空白,试试这两个小技巧:

  • 在代码最顶部开启错误显示,能直接看到具体报错:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
  • 去phpMyAdmin里手动执行SELECT * FROM tbladmin,确认表里面确实有数据;同时检查Uimages文件夹是否存在、图片路径是否正确。

按这个步骤改完,应该就能正常显示表格数据了,新手一定要养成加错误提示的习惯,不然出问题根本不知道哪里错!

内容的提问来源于stack exchange,提问作者Mahmood Ul Hassan

火山引擎 最新活动