简体   繁体   English

如何使用PHP连接到Mysql数据库?

[英]How to Connect to a Mysql database using PHP?

<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>

This is the code I am using to check if I am able to connect to Mysql. 这是我用来检查是否能够连接到Mysql的代码。

I am having problem connecting using this code. 我在使用此代码进行连接时遇到问题。 Should I change localhost to the name of the website? 我应该将localhost更改为网站的名称吗?

I tried ("www.abc.com","login username", "password") even this is not working. 我尝试了(“ www.abc.com”,“登录用户名”,“密码”),即使此操作无效。

EDIT: 编辑:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mobile4_you'@'cl79.blahblah.com' (using password: YES) in /home/mobilew4/public_html/mysql.php on line 2 Access denied for user 'mobile4_you'@'cl79.blahblah.com' (using password: YES) 警告:mysql_connect()[function.mysql-connect]:在第2行/home/mobilew4/public_html/mysql.php中对用户'mobile4_you'@'cl79.blahblah.com'的访问被拒绝(使用密码:是)用户“ mobile4_you” @'cl79.blahblah.com(使用密码:是)

If you're using something like cPanel, it will prefix your username and your database with your username for cpanel, for example: 如果您使用的是cPanel之类的东西,它将使用cpanel的用户名作为用户名和数据库的前缀,例如:

> cPanel login: foo 
> Database User: bar
> Database: ey

Your database user will be foo_bar and your database will be called foo_ey 您的数据库用户将为foo_bar,而您的数据库将被称为foo_ey

Localhost refers to where the database is running. Localhost是指数据库在哪里运行。 If it's running on the same server as your PHP server localhost should work fine. 如果它与PHP服务器localhost在同一台服务器上运行,则应该可以正常工作。 What kind of problems are you having? 你有什么问题? Are you sure that your username/password is correct? 您确定您的用户名/密码正确吗?

You should replace : 您应该更换:

  • localhost by the name of the server on which your MySQL server is localhost按您的MySQL服务器所在的服务器的名称)
    • if your MySQL server is on the same server as PHP, then it's localhost 如果您的MySQL服务器与PHP在同一台服务器上,则它是localhost
  • username by the login that allows you to access your database 登录username ,该登录username使您可以访问数据库
  • password by the corresponding password password由相应的密码

Of course, this is supposing you actually have a MySQL server up and running, with a user account created on it. 当然,这是假设您实际上已经启动并运行了一个MySQL服务器,并在其上创建了一个用户帐户。

Didn't you hosting company provide you with any information related to MySQL ? 您没有托管公司为您提供与MySQL相关的任何信息吗? Using those informations, what error message does mysql_error() give ? 使用这些信息, mysql_error()给出什么错误消息?

Do you have a mysql user which you've set up? 您有已设置的mysql用户吗? You'll need to do that. 您需要这样做。 In cPanel you can do it by clicking on MySQL databases, and on phpMyAdmin you can just add a new user on the start page. 在cPanel中,您可以通过单击MySQL数据库来执行此操作,而在phpMyAdmin上,您只需在开始页面上添加一个新用户即可。 Put that users' username and password in. 输入该用户的用户名和密码。

You need to pay attention to the error, if any, reported by mysql_error() . 您需要注意mysql_error()报告的错误(如果有mysql_error() This should give you some information you can use to diagnose the connection problem. 这应该为您提供一些可用于诊断连接问题的信息。

See this MySQL Forge wiki page about connection failure: 有关连接失败,请参见以下MySQL Forge Wiki页面:

http://forge.mysql.com/wiki/Error2003-CantConnectToMySQLServer http://forge.mysql.com/wiki/Error2003-CantConnectToMySQLServer

Or this page from the MySQL Manual: 或MySQL手册中的以下页面:

http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html

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

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