简体   繁体   English

尝试使用XAMPP访问计算机上的网站时发生PHP致命错误

[英]PHP Fatal Error when trying to access a website on my computer with XAMPP

I have set up a website on my PC with XAMPP, MySQL and Apache enabled. 我已经在启用XAMPP,MySQL和Apache的PC上建立了一个网站。 I am getting this fatal error. 我收到此致命错误。 I have set up the config files and followed the read-me correctly. 我已经设置了配置文件,并正确遵循了自述文件。 However I cannot seem to get where the problem is. 但是我似乎无法找到问题所在。 Database is correctly set up in config files and upload .sql to phpmyadmin. 在配置文件中正确设置了数据库,然后将.sql上传到phpmyadmin。

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\includes\global.php:29 Stack trace: #0 C:\xampp\htdocs\includes\global.php(107): db_connection() #1 C:\xampp\htdocs\admin\index.php(9): include_once('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\includes\global.php on line 29

Line 26-33 in global.php global.php中的26-33行

function db_connection()
{
    global $config,$data_sql;
    $data_sql = mysql_connect($config["sql_host"], $config["sql_user"], $config["sql_pass"]);
    if (!$data_sql) die("Can't connect to MySql");
    mysql_select_db($config["db_name"],$data_sql) or die("Can't select database");
}
function db_close()

mysql_connect is depreciated as far as i'm aware from php 7+. 据我所知,从php 7+开始,mysql_connect已贬值。 Try to use PDO or mysqli it's more secure and people will shout at you if you try to use mysql_connect if you're running an older version of php. 尝试使用PDOmysqli会更安全,如果您运行的是旧版php,则尝试使用mysql_connect时,人们会大喊大叫。 A quick google search would've found this quote in the php documentation. 快速的Google搜索将在php文档中找到此报价。

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 警告此扩展在PHP 5.5.0中已弃用,在PHP 7.0.0中已被删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLi或PDO_MySQL扩展。

In response to your comment, here's a PDO example of a typical connection to the database 为了回应您的评论,以下是与数据库的典型连接的PDO示例

<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "root";
$dbname = "dbname";

try{
$pdo = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername, 
$dbpassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
print "Error! Unable to connect: " . $e->getMessage() . "<br/>";
die();
}
?>

Edit the variables to suit your needs at the top, provided the details are correct the only thing that needs to be changed are the 在顶部编辑变量以适合您的需求,只要详细信息正确无误,唯一需要更改的是

$servername 
$dbusername 
$dbpassword 
$dbname 

myql_connect() is deprecated since php 5.5+ and removed in php 7. You can either use mysqli_connect() or pdo . 自php 5.5+起不推荐使用myql_connect(),在php 7中将其删除。您可以使用mysqli_connect()或pdo。 If you still want to use mysql_connect() try with php<7(which I wouldn't recommend) 如果您仍然想使用mysql_connect(),请尝试使用php <7(我不建议这样做)

Using mysqli , 使用mysqli

function db_connection()
{
    global $config,$data_sql;
    $data_sql = mysqli_connect($config["sql_host"], $config["sql_user"], $config["sql_pass"], $config["db_name"]); 
    // You can select the db, by passing it as 4th param if you like
    if (!$data_sql) die("Can't connect to MySql".mysqli_error($data_sql ) );

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM