検索
ホームページphp教程php手册PHP将MySQL的查询结果转换为数组并用where拼接的示例,mysqlwhere

PHP将MySQL的查询结果转换为数组并用where拼接的示例,mysqlwhere

mysql查询结果转换为PHP数组的几种方法的区别: 

  • $result = mysql_fetch_row():这个函数返回的是数组,数组是以数字作为下标的,你只能通过$result[0],$Result[2]这样的形式来引用。
  • $result = mysql_fetch_assoc():这个函数返回是以字段名为下标的数组,只能通过字段名来引用。$result['field1'].
  • $result = mysql_fetch_array():这个函数返回的是一个混合的数组,既可以通过数字下标来引用,也可以通过字段名来引用。$result[0]或者$result["field1"].
  • $result = mysql_fetch_object():以对象的形式返回结果,可以通过$result->field1这样的形式来引用。

建议使用mysql_fetch_assoc()或者mysql_fetch_array,这两个函数执行速度比较快,同时也可以通过字段名进行引用,比较清楚。 

where拼接技巧
将where语句从分支移到主干,解决where在分支上的多种情况,分支条件只需and 连接即可如where1==1等

$sql="SELECT * FROM bb where true ";

因为使用添加了“1=1”的过滤条件以后数据库系统就无法使用索引等查询优化策略,数据库系统将会被迫对每行数据进行扫描(也就是全表扫描)以比较此行是否满足过滤条件,当表中数据量比较大的时候查询速度会非常慢。优化方法
test.html

<td>商品名称:</td> 
<td width="200"><input type="text" class="text" name="kit_name" id="fn_kit_name"/></td> 
<td align="right">备案开始日期:</td> 
<td width="200"><input type="text" name="search[or_get_reg_date]"/><img  src="/static/imghwm/default1.png"  data-src="images/data.jpg"  class="lazy" / alt="PHP将MySQL的查询结果转换为数组并用where拼接的示例,mysqlwhere" ></td> 
<td>备案结束日期:</td> 
<td width="200"><input type="text" name="search[lt_reg_date]"/><img  src="/static/imghwm/default1.png"  data-src="images/data.jpg"  class="lazy" / alt="PHP将MySQL的查询结果转换为数组并用where拼接的示例,mysqlwhere" ></td> 
</tr> 
<tr> 
  <td>产品经理:</td> 
  <td><input type="text" class="text" name="search[managerid]"/></td> 

<&#63;php 
$postData = array( 
  'managerid' => '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<'09' AND reg_date>'2012-12-19' AND id in ('1','2','3') 

处理where条件的sql

<&#63;php 
/** 
 * 表单提交值转化成where拼接数组 
 */ 
function transArrayTerms($infoSearch) { 
  $aryRst = array(); 
  $separator = array('lt'=>'<', 'let'=>'<=', 'gt'=>'>', 'get'=>'>=', 'eq'=>'=', 'neq'=>'<>'); 
  foreach ($infoSearch as $term => $value) { 
    if (empty($value)) continue; 
 
    $name = $term; 
    if (strpos($term, "or_") !== false) { //添加or连接符 
      $terms['useOr'] = true; 
      $name = str_replace("or_", "", $term); 
    } 
 
    if (strpos($name, "in_") !== false) { 
      $terms['name'] = str_replace("in_", "", $name); 
      $terms['charCal'] = " in "; 
      $terms['value'] = "('" . implode("','", $value) . "')"; 
    } else { 
      $terms['name'] = $name; 
      $terms['charCal'] = " like "; 
      $terms['value'] = "'" . trim($value) . "%'"; 
    } 
    //放在else后面 
    foreach($separator as $charCalName =>$charCalVal){ 
      if (strpos($name, $charCalName."_") !== false) { 
        $terms['name'] = str_replace($charCalName."_", "", $name); 
        $terms['charCal'] = $charCalVal; 
        $terms['value'] = "'" . trim($value) . "'"; 
      } 
    } 
    $aryRst[] = $terms; 
    unset($terms); 
  } 
  return $aryRst; 
} 
 
function whereOperator($has_where, $useOr) { 
  $operator = $has_where &#63; ($useOr === false &#63; ' AND ' : ' OR ') : ' WHERE '; 
  return $operator; 
} 
 
/** 
 * aryTerm transArrayTerms转化后的查询条件 
 * @过滤没有输入的sql查询条件并转化成where条件. 
 */ 
function getWhereSql($aryTerm) { 
  $whereCause = ''; 
  if (count($aryTerm) > 0) { 
    $has_where = ''; 
    foreach ($aryTerm as $value) { 
      $has_where = whereOperator($has_where, isset($value['useOr'])); 
      $whereCause .= $has_where . $value['name'] . $value['charCal'] . $value['value']; 
    } 
  } 
  return $whereCause; 
} 
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)