MySQL与PHP的连接无法正常工作 [英] Connection of MySQL with PHP not working

查看:60
本文介绍了MySQL与PHP的连接无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

现在,我正尝试使用Apache服务器上的PHP文件连接到我的MySQL数据库:当我从终端运行PHP时,我的PHP可以连接到MySQL数据库(使用"php -f文件.php" ),但是当我从网页上执行它时,它只是无法连接.

I'm trying to connect to my MySQL database with a PHP file on my Apache server, now: my PHP can connect to the MySQL database when I run it from the terminal (using "php -f file.php") but when I execute it from a web page it just doesn't connect.

这是我的php文件:

echo "TRY CONNECTION";
$conn = mysql_connect($servername, $username, $password);
echo "EXECUTED CONNECTION";

Linux shell打印以下内容:

The Linux shell prints this:

尝试连接执行连接

TRY CONNECTIONEXECUTED CONNECTION

但是网页上会显示以下内容:

But the web page prints this:

:尝试连接

我尝试使用mysqli,但结果相同

I tried using mysqli but the result is the same

推荐答案

尝试一下

 echo "TRY CONNECTION";
 $conn =new mysqli($servername, $username, $password,$database);
 echo "EXECUTED CONNECTION";

或者可能是

$con = mysqli_connect("localhost","my_user","my_password","my_db");

这篇关于MySQL与PHP的连接无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆