首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

PHP 留言板 Fatal error: Call to undefined function inclue() 拼寫錯誤修正

     編輯:程式設計疑難解答

錯誤訊息

Fatal error: Call to undefined function inclue() in ...\lyb\index.php on line 2

根本原因

PHP 關鍵字 拼寫錯誤:inclue → 應為 include。

修正前後對比

<?php
// 錯誤
inclue ("config.php");

// 正確
include ("config.php");
// 或 require_once "config.php"; // 推薦:防止重複引入
?>

補充:config.php 連線代碼現代化建議

<?php
// config.php - 使用 PDO + 預處理防注入
$dsn = "mysql:host=localhost;dbname=guestbook;charset=utf8mb4";
$opt = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
];
try {
    $pdo = new PDO($dsn, "root", "root", $opt);
} catch (PDOException $e) {
    die("資料庫連線失敗: " . $e->getMessage());
}
?>

index.php 使用 PDO 查詢

<?php
require_once "config.php";
$stmt = $pdo->query("SELECT * FROM content");
$rows = $stmt->fetchAll();
?>
<h1>留言本</h1>
<table border="1" align="center" cellpadding="5" cellspacing="0">
    <tr><th>Name</th><th>Email</th><th>Content</th></tr>
    <?php foreach ($rows as $row): ?>
    <tr>
        <td><?= htmlspecialchars($row['name']) ?></td>
        <td><?= htmlspecialchars($row['email']) ?></td>
        <td><?= htmlspecialchars($row['content']) ?></td>
    </tr>
    <?php endforeach; ?>
</table>

關鍵改進點

  • 修正 inclue 拼寫錯誤。
  • 改用 PDO 替代已棄用的 mysql_* 函數。
  • 輸出前 htmlspecialchars() 防 XSS。
  • 使用 require_once 避免重複引入。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved