简体   繁体   English

Mysql 与 PHP 的连接给出 500 错误

[英]Mysql connection with PHP giving 500 error

I have a simple php file where I am opening my database connection.我有一个简单的 php 文件,用于打开我的数据库连接。

$con = mysql_connect( 'localhost', 'root', '12345' );
if ( ! $con ) {
   die( 'Could not connect: ' . mysql_error() );
} else {
   echo 'Connection established';
}

But when running the file I am getting the following error:但是在运行文件时,我收到以下错误:

localhost is currently unable to handle this request. localhost 当前无法处理此请求。 HTTP ERROR 500 HTTP 错误 500

But if I just try phpinfo();但如果我只是尝试phpinfo(); in the same file without having the above connection string it runs fine, which means my apache server is running already.在没有上述连接字符串的同一个文件中,它运行良好,这意味着我的 apache 服务器已经在运行。 I still tried by restarting but no luck!我仍然尝试通过重新启动但没有运气!

$ sudo service apache2 restart

I also tried the same code using mysqli but that didn't work either!我也使用mysqli尝试了相同的代码,但也没有用!

$con = mysqli( 'localhost', 'root', '12345' );

I can connect mysql from my terminal using the same above credential:我可以使用上述相同的凭据从我的终端连接mysql

$ mysql -u root p
$ password: 12345

mysql>

What I am missing?我缺少什么?

use this用这个

$con = mysqli_connect( 'localhost', 'root', '12345' );
if ( ! $con ) {
   die( 'Could not connect: ' . mysqli_error($con) );
} else {
   echo 'Connection established';
}

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

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