`
sunwinner
  • 浏览: 198207 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

PHP5.2.10连接MySQL

阅读更多

      最近有项目需要用PHP,于是利用业余的时间学习PHP,语法没什么问题,实战中马上卡壳了,遇到了和MySQL的连接问题。总结如下:

      1、和PHP4不同,PHP5中MySQL不再是内置支持的数据库了,安装的PHP的时候需要在php.ini中将

           extension=php_mysql.dll
           extension=php_mysqli.dll

      之前的分号(;)去掉,PHP4默认是没有分号的。

     

      2、上面的步骤之后,页面还是什么都不显示,连接测试的代码如下:

     

<?php 
    /* Connect to a MySQL server */ 
    $link = mysqli_connect( 
                'localhost',  /* The host to connect to  */ 
                'root',      /* The user to connect as*/ 
                'root',  /* The password to use */ 
                'books');    /* The default database to query */ 
    if (!$link) { 
       printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error()); 
       exit; 
    }

/* Send a query to the server */ 
    if ($result = mysqli_query($link, 'select * from books')) { 
        /* Fetch the results of the query  */ 
        while( $row = mysqli_fetch_assoc($result) ){ 
            printf("%s <br/>", $row['title']);
        } 
        /* Destroy the result set and free the memory used for it */ 
        mysqli_free_result($result); 
    } 
    /* Close the connection*/ 
    mysqli_close($link); 
?>

 程序运行到数据库连接的那一句就不再向下执行了。今天下班归来,在网上找了一些资料细细看了下,再仔细看我安装的PHP运行phpinfo()函数的输出,根本就没有MySQL的信息。解决方案是拷贝PHP安装目录下的libmysql.dll、php_mysqli.dll、php_mysql.dll到系统目录windows/system32下,我的测试代码需要php_mysqli.dll和libmysql.dll就可以了。万事开头难,再接再厉。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics