热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

phpmysql查询结果拼接_PHP将MySQL的查询结果转换为数组并用where拼接的示例_PHP

产品经理:21,or_get_reg_date09,lt_reg_date2012-12-19,in_idarray(1,2,3),);$tmpConditio

产品经理:

'21',

'or_get_reg_date' => '09',

'lt_reg_date' => '2012-12-19',

'in_id' => array(1, 2, 3),

);

$tmpConditions = transArrayTerms($postData);

echo $whereCause = getWhereSql($tmpConditions);

// WHERE managerid like '21%' OR reg_date'2012-12-19' AND id in ('1','2','3')

处理where条件的sql

&#39;&#39;<&#61;&#39;, &#39;gt&#39;&#61;>&#39;>&#39;, &#39;get&#39;&#61;>&#39;>&#61;&#39;, &#39;eq&#39;&#61;>&#39;&#61;&#39;, &#39;neq&#39;&#61;>&#39;<>&#39;);

foreach ($infoSearch as $term &#61;> $value) {

if (empty($value)) continue;

$name &#61; $term;

if (strpos($term, "or_") !&#61;&#61; false) { //添加or连接符

$terms[&#39;useOr&#39;] &#61; true;

$name &#61; str_replace("or_", "", $term);

}

if (strpos($name, "in_") !&#61;&#61; false) {

$terms[&#39;name&#39;] &#61; str_replace("in_", "", $name);

$terms[&#39;charCal&#39;] &#61; " in ";

$terms[&#39;value&#39;] &#61; "(&#39;" . implode("&#39;,&#39;", $value) . "&#39;)";

} else {

$terms[&#39;name&#39;] &#61; $name;

$terms[&#39;charCal&#39;] &#61; " like ";

$terms[&#39;value&#39;] &#61; "&#39;" . trim($value) . "%&#39;";

}

//放在else后面

foreach($separator as $charCalName &#61;>$charCalVal){

if (strpos($name, $charCalName."_") !&#61;&#61; false) {

$terms[&#39;name&#39;] &#61; str_replace($charCalName."_", "", $name);

$terms[&#39;charCal&#39;] &#61; $charCalVal;

$terms[&#39;value&#39;] &#61; "&#39;" . trim($value) . "&#39;";

}

}

$aryRst[] &#61; $terms;

unset($terms);

}

return $aryRst;

}

function whereOperator($has_where, $useOr) {

$operator &#61; $has_where ? ($useOr &#61;&#61;&#61; false ? &#39; AND &#39; : &#39; OR &#39;) : &#39; WHERE &#39;;

return $operator;

}

/**

* aryTerm transArrayTerms转化后的查询条件

* &#64;过滤没有输入的sql查询条件并转化成where条件.

*/

function getWhereSql($aryTerm) {

$whereCause &#61; &#39;&#39;;

if (count($aryTerm) > 0) {

$has_where &#61; &#39;&#39;;

foreach ($aryTerm as $value) {

$has_where &#61; whereOperator($has_where, isset($value[&#39;useOr&#39;]));

$whereCause .&#61; $has_where . $value[&#39;name&#39;] . $value[&#39;charCal&#39;] . $value[&#39;value&#39;];

}

}

return $whereCause;

}

本文原创发布php中文网&#xff0c;转载请注明出处&#xff0c;感谢您的尊重&#xff01;

相关文章

相关视频

网友评论

文明上网理性发言&#xff0c;请遵守 新闻评论服务协议我要评论



推荐阅读
author-avatar
飞航
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有