我正在处理一个项目,我收到 $host、$dbname、$user 和 $pass 的未定义错误。
但是只有在 dbConnect() 函数内部时才会出现错误。
这是代码(upload.php):
<?php
error_reporting(E_ALL);
require('config.php');
$filename = htmlentities($_FILES['file']['name']);
$tmpname = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
$filetype = $_FILES['file']['type'];
function dbConnect() {
try {
global $dbcon;
$dbcon = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
if (dbConnect()) {
print('worked');
}
配置.php:
<?php
global $host, $user, $pass, $dbname;
$host = "localhost"; // MySQL Hostname
$user = "root"; // MySQL User
$pass = "mypass"; // MySQL Password
$dbname = "files";