php mysql_connect 连接数据库失败解决方案

闲暇没事学习了一些php,结果今天连接库时怎么也连接不上,整整弄了一天总与找到一遍文章这才搞定:http://hi.baidu.com/shazi129/blog/item/e7aab644c8cd0c4a510ffeb1.html

 

上网查询得:把php_mysql.dll复制到php的ext文件中,把libmysql.dll复制到c盘的windows下。回到找我的php安装目录。里面并没有ext文件夹(我是安装的php,不是解压缩的),于是我怀疑安装有问题。立马重新安装一遍(直接覆盖就可以),果然发现,我安装的时候那些数据库默认的都是不加载的。重新安装了一遍这些文件都有了,按以上的方法进行了配置,查看了php的加载项(可以用echo phpinfo();查看php都加载了那些控件)结果还是不行,php并没有加载mysql,于是继续查找资料。找php的配置文件php.ini,因为我的php是安装的所以php.ini自动配置好了全部,并没有问题。(如果你是解压缩的需要手动配置mysql的相关内容:

[MySQL]


; Allow or prevent persistent links.
mysql.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
mysql.max_persistent = -1

; Maximum number of links (persistent + non-persistent).  -1 means no limit.
mysql.max_links = -1-->最多连接数

; Default port number for mysql_connect().  If unset, mysql_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order).  Win32 will only look
; at MYSQL_PORT.
mysql.default_port =3306---->端口号

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket =

; Default host for mysql_connect() (doesn't apply in safe mode).
mysql.default_host =localhost----->服务器地址

; Default user for mysql_connect() (doesn't apply in safe mode).
mysql.default_user =root--->数据库用户名

; Default password for mysql_connect() (doesn't apply in safe mode).
; Note that this is generally a *bad* idea to store passwords in this file.
; *Any* user with PHP access can run 'echo get_cfg_var("mysql.default_password")
; and reveal this password!  And of course, any users with read access to this
; file will be able to reveal the password as well.
mysql.default_password =root---->数据库登录密码

; Maximum time (in seconds) for connect timeout. -1 means no limit
mysql.connect_timeout = 60----->连接超时时间

; Trace mode. When trace_mode is active (=On), warnings for table/index scans and
; SQL-Errors will be displayed.
mysql.trace_mode = off

。。。。。。

[PHP_MYSQL]
extension=php_mysql.dll------>加载mysql库
[PHP_MYSQLI]
extension=php_mysqli.dll------>加载mysql扩展库

一切都弄完了,结果还是不行,无奈的只有少做暂停。。。。。。。

下午我又上网查了资料,发现了:http://hi.baidu.com/shazi129/blog/item/e7aab644c8cd0c4a510ffeb1.html;

终极解决方案是:把php中的libmysql.dll文件复制到Apache的bin里面,测试,ok了

 $link = @mysql_connect("localhost","root","root") or die("数据库链接失败!");

 if($link){
  echo "链接成功";
 }else{
  echo "链接失败2";
 }

你可能感兴趣的:(mysql,php,数据库,extension,passwords,warnings,php)