php总结《2》.docx
- 文档编号:4284206
- 上传时间:2023-05-06
- 格式:DOCX
- 页数:15
- 大小:20.72KB
php总结《2》.docx
《php总结《2》.docx》由会员分享,可在线阅读,更多相关《php总结《2》.docx(15页珍藏版)》请在冰点文库上搜索。
php总结《2》
php总结《2》
2010-08-1711:
32:
27| 分类:
php| 标签:
|字号大中小 订阅
文件处理
文件属性
file_exists('1.php')文件或目录是否存在
filesize()取得文件大小
is_readable()判断给定文件名是否可读
is_writable()判断给定文件名是否可写
is_executable()判断给定文件名是否可执行
filectime()获取文件的创造时间
filemtime()获取文件的修改时间
fileatime()获取文件的访问时间
stat()获取文件大部分属性值
解析目录
basename()返回路径中的文件名部分
dirname()返回目录
pathinfo()返回目录名、基本名和扩展名的关联数组
遍历目录
opendir()打开指定目录
readdir()关闭指定目录
closedir()关闭指定目录
rewinddir()倒回目录句柄
$dir_handle=opendir('.');
while($file=readdir($dir_handle))
{
echofilesize($file).'___'.$file.'
';
}
closedir($dir_handle);
建立和删除目录
mkdir()创建目录
rmdir()删除空目录
unlink()将目录中的每个文件都删除掉
文件操作
fopen()
fclose()
fwrite()写入文件
fputs()fwrite的别名
file_put_contents($文件名,$内容)把内容存成文件
file_get_contents()从文件读出内容
文件读取
fread()
fgets()从文件指针中读取一行
fgetc()从文件指针中读取字符
file()
readfile()读入一个文件并写入到输出缓冲
ftell()返回文件指针的当前位置
fseek()移动文件指针到指定的位置
rewind()移动文件指针到文件的开头
flock()文件锁定
copy()复制文件
unlink()删除文件
ftruncate()将文件截断到指定的长度
rename()重命名文件或目录
保存读取文件
-----------把内容存成文件
$cache_file=fopen('f:
\1.txt','w+');
fwrite($cache_file,$t);
-----------把内容存成文件
$s="内容";
file_put_contents('f:
/2.txt',$s);
-----------把文件内容读成字符串
$s=file_get_contents('f:
/2.txt');
echo$s;
-----------把文件内容按行读成字符串
$handle=@fopen("f:
/2.txt","r");
if($handle)
{
while(!
feof($handle))
{
$buffer=fgets($handle,4096);
echo$buffer.'
';
}
fclose($handle);
}
----------
session/cookie
setcookie("MyCookie[foo]",'Testing1',time()+3600)
session_start()
ini_set('session.cookie_lifetime',0);session对应cookie存活时间
ini_set('session.save_path','dir');
ini_set('session.save_path','2;session');session分两级存放
ini_set('session.name','SNS');
客户端禁用Cookie
session.use_trans_sid=1开启url传递sessionIdphp.ini
mysql
$link=mysql_connect('localhost','root','root')ordie(mysql_errno());
mysql_select_db('test')ordie(mysql_errno());
mysql_query('SETNAMESgbk');
$sql="SELECT*FROMtestLIMIT0,20";
$result=mysql_query($sql)ordie(mysql_errno());
while($msg=mysql_fetch_array($result)){
print_r($msg);
}
mysql_free_result($result);
mysql_close($link);
mysqli
查询
-------------------------------过程
$db_host="localhost"; //连接的服务器地址
$db_user="root"; //连接数据库的用户名
$db_psw="root"; //连接数据库的密码
$db_name="test";//连接的数据库名称
$mysqli=mysqli_connect($db_host,$db_user,$db_psw,$db_name);
mysqli_query($mysqli,'SETNAMESutf8');
$query="select*fromusers";
$result=mysqli_query($mysqli,$query);
while($row=mysqli_fetch_array($result))//循环输出结果集中的记录
{
echo($row['id'])."
";
echo($row['username'])."
";
echo($row['password'])."
";
echo"
";
}
mysqli_free_result($result);
mysqli_close($mysqli);
-------------------------------对象
$db_host="localhost"; //连接的服务器地址
$db_user="root"; //连接数据库的用户名
$db_psw="root"; //连接数据库的密码
$db_name="test";//连接的数据库名称
$mysqli=newmysqli($db_host,$db_user,$db_psw,$db_name);
$mysqli->query('SETNAMESutf8');
$query="select*fromusers";
$result=$mysqli->query($query);
if($result)
{
if($result->num_rows>0)//判断结果集中行的数目是否大于0
{
while($row=$result->fetch_array())//循环输出结果集中的记录
{
echo($row[0])."
";
echo($row[1])."
";
echo($row[2])."
";
echo"
";
}
}
}
else
{
echo"查询失败";
}
$result->free();
$mysqli->close();
增、删、改
$mysqli=newmysqli("localhost","root","root","sunyang");//实例化mysqli
$query="deletefromemployeewhereemp_id=2";
$result=$mysqli->query($query);
if($result){
echo"删除操作执行成功";
}else{
echo"删除操作执行失败";
}
$mysqli->close();
绑定结果
$mysqli=newmysqli("localhost","root","root","test"); //实例化mysqli
$query="select*fromusers";
$result=$mysqli->prepare($query); //进行预准备语句查询
$result->execute(); //执行预准备语句
$result->bind_result($id,$username,$password); //绑定结果
while($result->fetch()){
echo$id.'_';
echo$username.'_';
echo$password;
echo"
";
}
$result->close(); //关闭预准备语句
$mysqli->close(); //关闭连接
绑定参数
$mysqli=newmysqli("localhost","root","root","test"); //实例化mysqli
$query="insertintousers(id,username,password) values('',?
?
)";
$result=$mysqli->prepare($query);
$result->bind_param("ss",$username,$password); //绑定参数I:
integerD:
doubleS:
stringB:
blob
$username='sy0807';
$password='employee7';
$result->execute(); //执行预准备语句
$result->close();
$mysqli->close();
绑定参数、绑定结果
$mysqli=newmysqli("localhost","root","root","test"); //实例化mysqli
$query="select*fromuserswhereid
";
$result=$mysqli->prepare($query);
$result->bind_param("i",$id); //绑定参数
$id=10;
$result->execute();
$result->bind_result($id,$username,$password); //绑定结果
while($result->fetch()){
echo$id."_";
echo$username."_";
echo$password;
echo"
";
}
$result->close();
$mysqli->close();
多条查询语句
$mysqli=newmysqli("localhost","root","root","test"); //实例化mysqli
$query="selectidfromusers;";
$query.="selectidfromtest";
if($mysqli->multi_query($query)){ //执行多个查询
do{
if($result=$mysqli->store_result()){
while($row=$result->fetch_row()){
echo$row[0];
echo"
";
}
$result->close();
}
if($mysqli->more_results()){
echo("-----------------
"); //连个查询之间的分割线
}
}while($mysqli->next_result());
}
$mysqli->close();//关闭连接
pdo
查询
$db=newPDO('mysql:
host=localhost;dbname=test','root','root');
$sql="SELECT*FROMusers";
$result=$db->query($sql);
foreach($resultas$row)
{
var_dump($row);
}
$db=null;
增、删、改、事务开启
try
{
$db=newPDO('mysql:
host=localhost;dbname=test','root','root');
$db->beginTransaction();
$a=$db->exec("insertintousers(id,username,password)values('','Joe','Bloggs')");
if($a==false)
{
thrownewException("sql1执行失败");
}
$b=$db->exec("insertintousers(id,username,password,kkk)values('','Joe','Bloggs')");
if($b==false)
{
thrownewException("sql2执行失败");
}
$db->commit();
$db=null;
}
catch(Exception$ex)
{
echo$ex;
$db->rollback();
}
缓存
Memcache
.下载memcached,;2.解压,比如放在D:
\memcached-1.2.1;3.DOS下输入‘D:
\memcached-1.2.1\memcached.exe-dinstall’,进行安装(注意‘’不要输入);4.再次输入‘D:
\memcached-1.2.1\memcached.exe-dstart’启动memcached。
注意:
memcached以后会随机启动。
这样memcached就已经安装完毕了。
apache-conf-httph。
conf下面的LoadModulemem_cache_modulemodules/mod_mem_cache.so之前的#去掉
在php下面的php。
ini下面添加extension=php_memcache.dll
php下的ext下面复制php_memcache.dll文件
$memcache=newMemcache;
$memcache->addServer('172.19.5.199',11211);
$memcache->addServer('172.19.5.13',11211);
//$memcache->connect('localhost',11211)ordie("Couldnotconnect");
//$version=$memcache->getVersion();
//echo"Server'sversion:
".$version;
$memcache->set('key3',array(1,2,3));
var_dump($memcache->get('key3'));
ob
ob_start()
$content=ob_get_contents();
ob_clean();
$cache_file=fopen('f:
\1.html','w+');
fwrite($cache_file,$content);
页面静态化--------------------------------------
ob_start();
$static_file='1.html';//静态页面
$php_file=basename(__FILE__);//当前动态页面
if(!
file_exists($static_file)||
((filemtime($static_file)+10)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- php 总结
![提示](https://static.bingdoc.com/images/bang_tan.gif)