无法使用php连接到mysql [英] can't connect to mysql with php

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

问题描述

即使我可以使用phpmyadmin正常连接,我似乎也无法使用php脚本连接到mysql.我创建了一个具有密码的用户,并为该数据库提供了适当的特权,但是每次连接时它都会死,并说拒绝访问.我在Windows XP盒子上使用XAMPP.防火墙全部被禁用,我已经检查了用户名和密码是否正确.这是代码:

I can't seem to connect to mysql with a php script, even though I can connect fine with phpmyadmin. I created a user with a password, and gave it the proper priveleges for the db, but everytime it connects it dies saying access denied. I am using xampp on a windows xp box. Firewalls are all disabled, and I've checked the username nad password are correct. Here's the code:

$conn=mysql_connect('localhost','westbrookc16','megadots') || die (mysql_error());

用户名是否必须采用特定格式或其他格式?

Do usernames have to be in a specific format or something?

推荐答案

我很直觉,这里的问题是您授予它的主机,尽管这实际上只是有根据的猜测.如果您授予访问权限myuser@'127.0.0.1'或服务器的实际IP地址,则将不允许您使用localhost作为主机进行连接.这是由于以下事实:当将"localhost"指定为主机时,php将假定您要使用unix套接字而不是网络套接字,并且在这种情况下127.0.0.1与localhost不同.

I have a hunch that the problem here is the host you granted it to, though it's really not more than an educated guess. If you grant access myuser@'127.0.0.1' or the servers actual ip address, you won't be allowed to connect using localhost as host. This is due to the fact that when "localhost" is specified as host, php will assume that you want to use a unix socket instead of network sockets, and in that context 127.0.0.1 isn't the same as localhost.

mysql_connect()的手动条目中:

注意:每当您指定"localhost"时 或以"localhost:port"作为服务器, MySQL客户端库将被覆盖 这并尝试连接到本地 套接字(在Windows上为命名管道).如果你 要使用TCP/IP,请使用"127.0.0.1" 而不是"localhost".如果是MySQL 客户端库尝试连接到 错误的本地套接字,应设置 正确的路径作为运行时配置 在您的PHP配置中并离开 服务器字段为空白.

Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as Runtime Configuration in your PHP configuration and leave the server field blank.

希望这不是完全多余的. :)

Hope this isn't totally redundant. :)

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

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