如何从php连接到mysql? [英] How do I connect to mysql from php?

查看:130
本文介绍了如何从php连接到mysql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究有关php/mysql开发的书中的示例. 我正在linux/apache环境中工作.

I'm working through examples from a book on php/mysql development. I'm working on a linux/apache environment.

我已经建立了一个数据库和一个用户.我尝试连接以下代码:

I've set up a database and a user. I attempt to connect with this line of code:

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

我收到此错误:

警告:mysql_connect() [function.mysql-connect]:访问 用户'www-data'@'localhost'被拒绝 (使用密码:是)在 /var/www/hosts/dj/connect.php在线 3无法连接数据库: 拒绝用户访问 'www-data'@'localhost'(使用 密码:是)

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: YES) in /var/www/hosts/dj/connect.php on line 3 unable to connect to database: Access denied for user 'www-data'@'localhost' (using password: YES)

我只能猜测这里发生了什么: 我认为www-data是apache的用户名.在数据库连接后,传递给mysql的凭据不是我的数据库用户的凭据,而是apache自己的凭据.那是这里发生的事吗?

I can only guess what is happening here: I think www-data is a username for apache. Upon the database connection, the credentials being passed in to mysql are not those of my database user, but rather apache's own credentials. Is that what is happening here?

如何传递为用户定义的凭据?

How do I pass in the credentials I've defined for my user ?

顺便说一句-我确实在$ db_hostname,$ db_username,$ db_password变量中有凭据.

edit: By the way - I do have credentials in the variables $db_hostname, $db_username, $db_password.

它们通过require_once被另一个文件传递.如果找不到该文件,则出现错误.因此,我知道我的脚本正在使用我的用户名和密码.

they are passed in by another file using require_once. If that file can't be found, then I get an error. So, I know that my username and password are being used by my script.

我的两个脚本都可以在这里看到: http://pastebin.com/MUneLEib

Both my scripts can be seen here: http://pastebin.com/MUneLEib

已解决:

谢谢大家.

你们中的一些人指出我的编码是粗心的.

A couple of you pointed out that I had coded carelessy.

此外,我对Neo的回答特别满意:他告诉我为什么使用apache进程所有者的用户名.

Also, I was particularly pleased by Neo's answer: he told me why the username of the owner of the apache process was being used.

:)

推荐答案

login.php中使用变量$database_username,但是在连接函数中使用$db_username.尝试匹配它们.

In your login.php you use the variable $database_username, but in your connection function you use $db_username. Try matching them up.

这篇关于如何从php连接到mysql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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