Home  >  Article  >  Backend Development  >  PHP知识点笔记

PHP知识点笔记

WBOY
WBOYOriginal
2016-06-23 14:37:291005browse

显示错误:PDOException could not find driver.

是表示PDO没有安装对应数据库的扩展,比如没有安装PDO_mysql

http://pecl.php.net/package/PDO_MYSQL

下载源码

phpize

./configure --with-php-config=/usr/local/php/bin/php-config
./make

./make install 

phpExcel的使用需要使用到php的xmlreader和xmlwriter扩展

如果你是使用yum安装php

就直接使用yum install php-xml来进行安装

php的define不能重复定义

比如:

<?phpdefine("TEST", "11");define("TEST", "22");echo TEST;
 
[yejianfeng@xen193v ~/handcode]$ php test.php PHP Notice:  Constant TEST already defined in /home/yejianfeng/handcode/test.php on line 4Notice: Constant TEST already defined in /home/yejianfeng/handcode/test.php on line 411
PHP如何获取本地所有变量

$localVariables = compact(array_keys(get_defined_vars()));

安装php模块可以这么安装

PHP_PDO_SHARED=1 pecl install pdo_mysql

mysql的字符编码

mysql一般默认的客户端字符编码为:latin1

mysql的字符编码有几种:

mysql> show variables like "character_set_%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

其中信息输入路径是

client - connection - server

信息输出路径是

server - connection - result

输入set names utf8是临时设置client和connection的字符编码

如何在PHP中初始化mysql的字符编码?

有两种方法可以初始化设置:

1

        $pdo = new PDO("mysql:host={$config['host']};dbname={$config['dbname']};port={$config['port']}",             $config['user'], $config['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

2

   $pdo = new PDO("mysql:host={$config['host']};dbname={$config['dbname']};port={$config['port']}",             $config['user'], $config['password']);        $pdo->exec('set names utf8');
PHP的PDO扩展

使用php如果要使用PDO,你需要安装的扩展除了PDO,还要各种数据库的驱动,PDO扩展是定义了一系列接口,但是你不可以使用PDO扩展直接操作数据库,需要安装制定PDO驱动扩展

PDO扩展的说明:http://www.php.net/manual/en/intro.pdo.php

PDO扩展只能在PHP 5.0之上使用(使用了php的OO属性)

针对不同的数据库的扩展有:

为什么PDO的prepare不能打出最后的sql

使用PDO一直有一个郁闷的地方,就是使用prepare的时候想要将最后执行的sql打出来做log,发现没有方法。。。

后来想了想,prepare是mysql的语句,那么PDO的prepare和execute实际上是对mysql发送了几次请求,prepare,set,execute

mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';mysql> SET @a = 3;mysql> SET @b = 4;mysql> EXECUTE stmt1 USING @a, @b;+------------+| hypotenuse |+------------+|          5 |+------------+mysql> DEALLOCATE PREPARE stmt1;

所以,如果要打出log,只有自己拼sql了。。。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn