php的慢速日志引起的Mysql 2013错误怎么办

 更新时间:2016年11月25日 15:34  点击:2362
php的慢速日志引起的Mysql 2013错误怎么办,下面我们就一起来看看这个问题的解决办法,希望例子能够帮助到各位。


Description:
————
If mysql query is longer as request_slowlog_timeout, connection breaks.

Test script:


<?php
// request_slowlog_timeout = 10s  (at /etc/php5/fpm/php-fpm.conf)
 
// $mysqli =
// ...
$query = "SELECT SLEEP (15)";
 
$res = $mysqli->query($query);
if (!$res) {
    echo $mysqli->error; // Error Code: 2013. Lost connection to MySQL server during query
    exit;
}
Expected result:
—————-
connection must be preserved and the request should be executed

in_array函数是判断数据中是否存在指定的内容了,对于这个函数用法非常的简单但在使用过程中会我发现有一些问题,下面我们就对于这些问题来看看如何处理吧。


先介绍一下需求背景:

发票方式:
0=捐赠(不要问我为什么,历史原因)
1=对中寄送
2=索取
3=电子发票

现在要对用户提交的数据进行检测:

if(!in_array($_POST['invoice_action'], array(0,1,2,3))){
    throw new Exception('请选择正确的发票方式');
}

这个时候出现一个问题,如果压根就不存在$_POST[‘invoice_action’]这个值,为什么没有抛出异常?
经确认,这就是PHP作为弱类型语言的一个坑!!!没错,这是一个坑!!!
看一下这组代码:


echo in_array('', array(0)) ? 1 : 0;     // 结果:1
echo in_array(null, array(0)) ? 1 : 0;   // 结果:1
echo in_array(false, array(0)) ? 1 : 0;  // 结果:1

这么大一个坑,我们要怎么绕过或者填起呢?
方法一:in_array支持第三个参数,强制对数据类型检测


echo in_array('', array(0), true) ? 1 : 0;     // 结果:0
echo in_array(null, array(0), true) ? 1 : 0;   // 结果:0
echo in_array(false, array(0), true) ? 1 : 0;  // 结果:0

方法二:依然是数据类型方向,把数组中的0改为字符串


echo in_array('', array('0'), true) ? 1 : 0;     // 结果:0
echo in_array(null, array('0'), true) ? 1 : 0;   // 结果:0
echo in_array(false, array('0'), true) ? 1 : 0;  // 结果:0

网页采集现在用到最多是工具了,像最受站长欢迎的就是火车头了,但有一些站长喜欢使用网页来自定义采集了,下面一起来看一个php 网页采集入库程序代码

php 网页采集程序总结,最近帮朋友做了个采集程序

以www.xxxx.com/shop_list.php?page=1&province=%B1%B1%BE%A9为例

%B1%B1%BE%A9是gb2312的转码,例如

$aa=”北京”;
$aa = @iconv(“utf-8″, “gb2312″,$aa);
echo $bb=urlencode($aa);

我们通过file_get_contents($url) 抓取网页 当然也可以是curl

function getHtml($url){
$ch2 = curl_init($url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch2);
curl_close($ch2);
return $html;
}

抓取我们想要的页面数据,可以设定从哪个位置到哪个位置的区间,取出中间数据,通过以下方法实现

function findneed($wholestr,$strkey1,$strkey2)
{
$num1 = strpos($wholestr , $strkey1)+strlen($strkey1);
$num2 = strpos($wholestr ,$strkey2);
$needstr =substr($wholestr ,$num1,$num2-$num1 );
return $needstr;
}
当然这是一种方法,我们只要写出一个php即可,根据分页抓取,但是如果都放在循环里面,岂不是很慢

我们介绍另个算法

<script>
location.href=”index.php?page=<?=$page?>&provinceIndex=<?=$provinceIndex?>&totalPage=<?=$totalPage?>”;
</script>

通过实现网页跳转页数,抓取,访问程序,不断跳转页数,把当前url的page 数组保存到数据库

其他的无非是些正则表达式的用法:

比如我们想取页面中的所有城市

province

可用preg_match_all(‘/<select name=”province”(.*?)>(.*?)<\/select>/s’,$html,$selects);即可

(.*?)表示任意字符   . 是任何东西 * 是0至无限  ? 是0至1

还有种算法是递归,类似循环取值

function collectionProvinceData($url,$province,$page=1,$totalPage=-1){
if($page>$totalPage&&$totalPage>-1){
return false;
}
$collectionUrl = $url."?page=".$page."&province=".urlencode(iconv('UTF-8', 'GB2312', $province));
echo "当前url:".$province."第{$page}页 url".$collectionUrl."<hr/>";
$html = getHtml($collectionUrl);
$html = mb_convert_encoding($html, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
if($totalPage==-1){
$latestPageNum = getLatestPageNum($html);
if($latestPageNum>0){
$totalPage = $latestPageNum;
}
}
$dataRows = getDataRows($html);
saveDataRowsOrNot($dataRows);
if(!empty($dataRows)){
$page++;
}
ob_flush();
flush();
collectionProvinceData($url,$province,$page,$totalPage);
}

缩略图可以通过gd库来实现,下面我们一起来看一个简单的php使用GD库实现文字图片水印及缩略图例子,希望此例子能够为大家带来有效的帮助。


我们要使用gd库就必须先打开gd库,具体如下

Windows下开启PHP的GD库支持

找到php.ini,打开内容,找到:

;extension=php_gd2.dll

把最前面的分号“;”去掉,再保存即可,如果本来就没有分号,那就是已经开启了。


具体可以参考下文:http://www.111cn.net/phper/php/48352.htm


一:添加文字水印 使用方法

require 'image.class.php'
$src="001.jpg";
$content="hello";
$font_url="my.ttf";
$size=20;
$image=new Image($src);
$color=array(
0=>255,
1=>255,
2=>255,
2=>20
);
$local=array(
'x'=>20,
'y'=>30
);
$angle=10;
$image->fontMark($content,$font_url,$size,$color,$local,$angle);
$image->show();

二:图片缩略图 使用方法:

require 'image.class.php'
$src="001.jpg";
$image=new Image($src);
$image->thumb(300,200);
$image->show();

三:image.class.php

class image{
private $info;
private $image;
public function __contruct($src){
$info= getimagesize($src);
$this->info=array(
'width'=> $info[0],
'height'=>$info[1],
'type'=>image_type_to_extension($info[2],false),
'mime'=>$info['mime'],
 
);
$fun="imagecreatefrom{$this->info['type']}";
 
$this->image= $fun($src);
 
}
 
//缩略图
public function thumd($width,$height){
$image_thumb= imagecreatetruecolor($width,$height);
imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']);
imagedestroy($this->image);
$this->image=$image_thumb;
 
}
//文字水印
public function fontMark($content,$font_url,$size,$color,$local,$angle){
 
$col=imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
$text=imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);
}
//输出图片
public function show()
{
header("Content-type:",$this->info['mime']);
 
$func="image{$this->info['type']}";
$func($this->image);
 
}
public function save($nwename){
$func="image{$this->info['type']}";
//从内存中取出图片显示
$func($this->image);
//保存图片
$func($this->image,$nwename.$this->info['type']);
 
}
public function _destruct(){
 
imagedestroy($this->image);
}
 
}

微信开发已经是现在程序员必须要掌握的一项基本的技术了,其实做过微信开发的都知道微信接口非常的强大做起来也非常的简单,这里我们一起来看一个微信自动登陆注册的例子。


php 微信扫码 pc端自动登陆注册 用的接口scope 是snsapi_userinfo,微信登陆一个是网页授权登陆,另一个是微信联合登陆

网页授权登陆:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

微信联合登陆:https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN

一:首先把微信链接带个标识生成二维码

比如链接为 https://open.weixin.qq.com/connect/oauth2/authorize?appid=’.$appid.’&redirect_uri=’.$url.’&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect’  我们可以在state上做文章,因为state你传入什么微信那边返回什么

可以作为服务器与微信段的一个标识

public function creatqrAction(){
 
if($_GET['app']){
$wtoken=$_COOKIE['wtoken'];
$postdata=$_SESSION['w_state'];
if($wtoken){
$postdata=$wtoken;
}
include CONFIG_PATH . 'phpqrcode/'.'phpqrcode.php'
$sh=$this->shar1();
$value="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx138697ef383a9167&redirect_uri=http://www.xxx.net/login/wcallback&response_type=code&scope=snsapi_userinfo&state=".$postdata."&connect_redirect=1#wechat_redirect";
 
$errorCorrectionLevel = "L";
 
$matrixPointSize = "5";
 
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
}
 
}

此时生成了二维码 state是标识,phpqrcode可以在文章末尾下载,这样我们设置了回调地址http://www.xxx.net/login/wcallback

就可以在wcallback方法里面处理数据 插入用户 生成session,跳转登陆,pc端可以设置几秒钟ajax请求服务器,一旦获取到了

state,即实现调整,微信浏览器里处理完后可以关闭窗口,微信js可实现

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
WeixinJSBridge.call('closeWindow');
 
}, false);

也可以授权登陆成功后跳转到微信服务号关注页面

header("Location: weixin://profile/gh_a5e1959f9a4e");

wcallback方法做处理登陆

$code = $_GET['code'];
$state = $_GET['state'];
$setting = include CONFIG_PATH . 'setting.php'
$appid=$setting['weixin']['appid'];
$appsecret=$setting['weixin']['appsecret'];
 
if (empty($code)) $this->showMessage('授权失败');
try{
 
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'
 
$token = json_decode($this->https_request($token_url));
 
}catch(Exception $e)
{
print_r($e);
}
 
if (isset($token->errcode)) {
echo '

错误:

'.$token->errcode;
echo '

错误信息:

'.$token->errmsg;
exit;
}
 
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo '

错误:

'.$access_token->errcode;
echo '

错误信息:

'.$access_token->errmsg;
exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'
//转成对象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo '

错误:

'.$user_info->errcode;
echo '

错误信息:

'.$user_info->errmsg;
exit;
}
//打印用户信息
// echo '

'
// print_r($user_info);
// echo '

'

phpqrcode类库下载在此不提供各位可以百度搜索下载

magento微信扫码网站自动登录的例子

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN

查看授权后接口调用(UnionID),不难发现填写回调地址,用户确认登陆pc端即可跳转

获取UnionID方法


public function wcallbackAction(){
 
$code = $_GET['code'];
$state = $_GET['state'];
$setting = include CONFIG_PATH . 'setting.php';
$appid=$setting['weixin']['appid'];
$appsecret=$setting['weixin']['appsecret'];
 
if (empty($code)) $this->showMessage('授权失败');
try{
 
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
 
$token = json_decode($this->https_request($token_url));
 
}catch(Exception $e)
{
print_r($e);
}
 
if (isset($token->errcode)) {
echo '<h1>错误:</h1>'.$token->errcode;
echo '<br/><h2>错误信息:</h2>'.$token->errmsg;
exit;
}
 
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo '<h1>错误:</h1>'.$access_token->errcode;
echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg;
exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
//转成对象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo '<h1>错误:</h1>'.$user_info->errcode;
echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg;
exit;
}
//打印用户信息
// echo '<pre>';
// print_r($user_info);
// echo '</pre>';
 
//获取unionid
 
$uid=$user_info->unionid;
 
}
 
//用户操作处理 分为再次登录和第一次登陆
 
$sql="select h_user_id from dtb_user_binded as t1 left join dtb_user_weixin as t2 on t1.u_id=t2.id where t1.u_type='".
User::$arrUtype['weixin_num_t']."' and t2.openid='$user_info->unionid'";
$h_user_id = Core_Db::getOne($sql);
if(!empty($h_user_id)){//该weixin号再次登录
 
}{//该weixin号第一次登录
 
}

[!--infotagslink--]

相关文章

  • Ecshop提示Only variables should be passed by reference in错误

    在安装好ecshop软件之后我们打开首页时提示Only variables should be passed by reference in错误了,碰到这个问题是什么原因呢?下面我们就一起来看看解决办法吧。...2016-11-25
  • 409错误是什么 http 409错误怎么解决

    409错误是什么?http 409错误怎么解决呢?不少站长在遇到这个错误代码之后都一筹莫展,本次一聚教程网为大家带来了详细的说明,快来看看吧。 409错误是什么: HTTP 40...2017-01-22
  • 414错误是什么 414错误怎么解决

    414错误是HTTP协议状态码中的一种,很多都还不知道414错误是什么,以及不知道怎么解决414错误,那么就来看看小编带来的介绍吧。 414错误是什么: HTTP 414错误,(Requ...2017-01-22
  • http 405错误是什么 http 405错误怎么解决

    http 405错误是什么?http 405错误怎么解决?相信很多站长都在找这两个问题的答案,本次小编为大家带来了详细的教程,快来看看吧。 405错误是什么: HTTP 405错误是H...2017-01-22
  • 401错误码代表什么 401错误解决办法

    401是HTTP状态码的一种,属于“请示错误”,表示请求可能出错,已妨碍了服务器对请求的处理。具体的401错误是指:未授权,请求要求进行身份验证。登录后,服务器可能会返回对页面...2017-01-22
  • http 402错误是什么 http 402简介

    http 402错误是什么?402错误较为少见,一般不轻易出现,下面小编就来告诉大家402错误是什么吧。 HTTP 402错误是HTTP状态码的一种,表示“要求付费”; 所求的...2017-01-22
  • 411错误是什么 411错误怎么解决

    411错误是HTTP协议状态码的一种,很多人都还不知道411错误是什么,本次一聚教程网将为大家进行解答,并且告诉大家411错误怎么解决。 411错误是什么: HTTP 411错误,(Lengt...2017-01-22
  • apache网站提示503错误解决办法

    Apache status 503 的原因大致有如下几种情况 : 1、 CPU 负载过高,服务器响应不过来,返回503 2、 系统连接数超限,超过MaxVhostClients的上限,返回503 3、 单IP连接数超限,超过M...2016-01-28
  • 404错误是什么 404错误怎么解决

    403错误是网站访问过程中,常见的错误提示。资源不可用,服务器理解客户的请求,但拒绝处理它。通常由于服务器上文件或目录的权限设置导致,比如IIS或者apache设置了访问权限...2017-01-22
  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • 403错误是什么 403错误怎么解决

    403错误是HTTP状态码的一种,属于“请示错误”,表示服务器拒绝请求。如果在搜索引擎尝试抓取您网站上的有效网页时显示此状态代码,那么,这可能是您的服务器或主机拒绝搜索...2017-01-22
  • 412错误是什么 412错误怎么解决

    412错误是什么?412错误怎么解决?本次一聚教程网将为大家带来详细的介绍,帮助大家全面了解412错误的意思以及解决412错误的方法。 412错误是什么: HTTP 412错误,(Precond...2017-01-22
  • HTTP 408错误是什么 HTTP 408错误解决方法

    相信很多站长都遇到过这样一个问题,访问页面时出现408错误,下面一聚教程网将为大家介绍408错误出现的原因以及408错误的解决办法。 HTTP 408错误出现原因: HTT...2017-01-22
  • 407错误是什么 407错误怎么解决

    407错误是什么?407错误怎么解决?不少站长都遇到过407错误,下面小编将告诉大家如何处理407错误。 407错误是什么: HTTP 407错误是HTTP协议状态码的一种,表示需要代...2017-01-22
  • 406错误是什么 406错误怎么解决

    HTTP 406错误是HTTP协议状态码的一种,表示无法使用请求的内容特性来响应请求的网页。一般是指客户端浏览器不接受所请求页面的 MIME 类型。 而MIME类型是在把输出...2017-01-22
  • 410错误是什么 http 410错误怎么解决

    410错误是HTTP协议状态码的一种,本次一聚教程网将为大家详细介绍HTTP 410错误是什么,以及410错误的解决办法。 410错误是什么: HTTP 410错误是HTTP协议状态码的...2017-01-22
  • 详解Mysql中的JSON系列操作函数

    新版 Mysql 中加入了对 JSON Document 的支持,可以创建 JSON 类型的字段,并有一套函数支持对JSON的查询、修改等操作,下面就实际体验一下...2016-08-23
  • HTTP 400错误是什么 HTTP 400错误怎么解决

    每当遇到http错误代码为400,代表客户端发起的请求不符合服务器对请求的某些限制,或者请求本身存在一定的错误,那么HTTP 400错误怎么解决呢?请看下文介绍。 目前400错...2017-01-22
  • PHP Fatal error: Cannot use object of type stdClass as array in错误

    下面一起来看看在php开发中碰到PHP Fatal error: Cannot use object of type stdClass as array in错误问题的解决办法吧。 普通的数组出现如下错误 代码...2016-11-25
  • Laravel 调试工具 laravel-debugbar 打印日志消息

    laravel-debugbar 调试工具的教程小编整理了几篇不错的教程,今天我们来看一篇Laravel 调试工具 laravel-debugbar 打印日志消息例子,希望文章对各位有帮助。 其实不...2016-11-25