普通网友 2010-04-05 03:13
浏览 28

如何从php连接到mysql?

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);

I get this error:

Warning: mysql_connect() 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)

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 ?

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

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.

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

#

Solved:

Thanks guys.

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

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.

:)

  • 写回答

10条回答 默认 最新

  • donk68254 2010-04-05 03:15
    关注

    The username goes into $db_username, and the password goes into $db_password.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集