如何获取与特定值匹配的特定类型的行?

时间:2012-04-03 06:55:51

标签: php mysql

我想从MySQL数据库中的表中获取数据。

  $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
  mysql_select_db($database, $linkID) or die("Could not find database."); 

 $search='flu';

  $query = mysql_query("SELECT * From ProviderDxCptCodes WHERE CodeType='CPT'");

   $rows = array();
   while($row = mysql_fetch_assoc($query)) {
   $rows[] = $row;
  }
  echo json_encode($rows);

我希望此查询向我显示与搜索字符串flu匹配的所有CPT代码。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

你的意思是什么?

SELECT * From ProviderDxCptCodes WHERE CodeType='CPT' AND name LIKE '%flu%';

答案 1 :(得分:0)

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
  mysql_select_db($database, $linkID) or die("Could not find database."); 


  $flu = "CPT";

  $query = mysql_query("SELECT * From ProviderDxCptCodes WHERE CodeType='%".mysql_real_escape_string($flu)."%'");

   $rows = array();
   while($row = mysql_fetch_assoc($query)) {
   $rows[] = $row;
  }
  echo json_encode($rows);