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
耗費了我好一會兒時間,但願路過的朋友看到,少走彎路。