Windows安装Apache 2.2.17、PHP 5.3.5与MySQL
1、自动安装apache 2.2.17
apache下载页面:http://httpd.apache.org/download.cgi
测试apache:访问http://localhost/
2、手动安装php 5.3.5
php下载页面:http://windows.php.net/download/
选择VC6 zip版本下载,解压缩到C:\Program Files\PHP\。
复制C:\Program Files\PHP\php.ini-development,粘贴为C:\Program Files\PHP\php.ini
修改C:\Program Files\PHP\php.ini
搜索;extension_dir = "ext"
把前面的分号去掉,改为
extension_dir = "C:\Program Files\PHP\ext"
搜索
;extension=php_pdo
改为
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_sqlite.dll
extension=php_sqlite3.dll
搜索
timezone
改为
date.timezone = asia/shanghai
连接apache与php:
修改C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf
在最下面加入:
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/Program Files/PHP/"
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
AddHandler application/x-httpd-php .php
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
搜索DirectoryIndex
改为
DirectoryIndex index.html index.php
重启apache,测试:apache与php
新建文件C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpinfo.php
<?php
phpinfo();
?>
3、自动安装MySQL(安装过程中勾选环境变量)
测试mysql:
使用命令行:mysql -uroot -p1
测试php连接mysql:
<?php
mysql_connect("127.0.0.1","root","1") or die(mysql_error());
var_dump($connet); ?>
测试PDO连接mysql:
<?php
class db extends PDO
{
public static $config_1 = array
(
'dbms' => 'mysql',
'host' => '127.0.0.1',
'db_name' => 'mysql',
'user' => 'root',
'pass' => '1',
'db_charset' => 'utf8',
);
public function __construct($config_name='config_1')
{
switch($config_name)
{
case 'config_1':
$config = self::$config_1;
break;
case 'config_2':
$config = self::$config_2;
break;
}
$dsn = $config['dbms'].':host='.$config['host'].';dbname='.$config['db_name'].';charset='.$config['db_charset'];
try
{
parent::__construct($dsn,$config['user'],$config['pass']);
PDO::exec("SET NAMES 'utf8';");
}
catch(PDOException $e)
{
die("Error: ".$e->__toString()."<br/>");
}
}
public final function query($sql)
{
try
{
return parent::query($this->setString($sql));
}
catch(PDOException $e)
{
die("Error: ".$e->__toString()."<br/>");
}
}
private final function setString($sql)
{
echo $sql."<br/>";
return $sql;
}
}
$db = new db();
$sql = 'SELECT * FROM user LIMIT 1';
$result = $db->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
$fetch_result = $result->fetchAll();
var_dump($fetch_result);
?>
重要提醒:
不要使用php的自动安装包(msi后缀名),里面的文件和手动安装包(zip后缀名)不一样。自动安装包会导致一些奇怪的问题,就是因为里面的文件错误。
提醒:php 5.3中没有libmysql.dll了,不需要了,直接就能连MySQL,现在是内置支持了。
参考资料:http://www.cnblogs.com/veryinf/archive/2009/10/13/1537269.html
耗费了我好一会儿时间,但愿路过的朋友看到,少走弯路。