[英]Cannot connect to MYSQL using PHP
I could not connect to the local host MYSQL database using my simple php code and DOES NOT display anyything. 我无法使用简单的php代码连接到本地主机MYSQL数据库,并且不显示任何内容。 THis is applicable to all users and root.Eventhough the problem is reported many times, i could not fix it.
这适用于所有用户和root用户。尽管已多次报告该问题,但我无法修复。 Also it is written in one site that it may have to make changes in php file.Please give me solution for this connection error.
另外它写在一个站点上,它可能不得不在php文件中进行更改。请给我解决此连接错误的解决方案。
I use php 5.5.9-1 in Ubuntu. 我在Ubuntu中使用php 5.5.9-1。 MYSQL version is 5.5.38.
MYSQL版本是5.5.38。 My code is
我的代码是
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$conn = mysql_connect($servername, $username, $password);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Mysql
is deprecated. 不建议使用
Mysql
。 Use Mysqli
. 使用
Mysqli
。
Create a file and name it as db_connect.php
and copy the below code. 创建一个文件并将其命名为
db_connect.php
并复制以下代码。
<?php
$connection = mysqli_connect("localhost","username","password","database_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Open browser and type http://localhost/db_connect.php
. 打开浏览器,然后输入
http://localhost/db_connect.php
。 If you see a blank page, your connection is fine. 如果看到空白页,则连接正常。 If there's a problem, it will show you the error.
如果有问题,它将显示错误。
Call to undefined function mysql_connect()
调用未定义的函数mysql_connect()
This error message from your comments tells me that you do not have the MySQL extension installed. 您评论中的此错误消息告诉我您尚未安装MySQL扩展。
Execute this in your terminal to install: sudo apt-get install php5-mysql
在您的终端中执行此
sudo apt-get install php5-mysql
以进行安装: sudo apt-get install php5-mysql
Then restart Apache: sudo /etc/init.d/apache2 restart
然后重新启动Apache:
sudo /etc/init.d/apache2 restart
Please be aware that the "mysql_" functions are marked as deprecated and your script might fail with a future PHP update. 请注意,“ mysql_”函数被标记为已弃用,并且您的脚本可能会在以后的PHP更新中失败。 Please use "mysqli_" or PDO.
请使用“ mysqli_”或PDO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.