PDO / mysqli查询不会返回与正则表达式的mysql cli相同的行

时间:2018-06-08 20:51:24

标签: php mysql regex pdo

我遇到了PHP PDO的一些问题,也发生在mysqli

我有一个在mysql cli中返回221行的mysql查询但是如果我在php中运行相同的查询,pdo查询只返回25行并且是不正确的行,

$stmt = $pdo->query("SELECT id, company_phone, locale, headquarters FROM xmltest.company where company_phone regexp '^\\([0-9]+\\)\\ [0-9]+\\ [0-9]' = 1 and char_length(company_phone) = 14");

   /* SELECT id, company_phone, locale, headquarters FROM xmltest.company where 
company_phone regexp '^\\([0-9]+\\)\\ [0-9]+\\ [0-9]' = 1 and 
char_length(company_phone) = 14  */

正则表达式与(021)551 9771等手机相匹配  但在pdo返回电话,如64 06 358 7361

我怀疑是与反斜杠逃脱相关但不确定的东西。

1 个答案:

答案 0 :(得分:1)

你需要加倍反斜杠,因为MySQL需要两个反斜杠来匹配文字反斜杠。此外,您需要将import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.ScrollView; public class FloorPlan extends AppCompatActivity { private ScaleGestureDetector scaleGestureDetector; private float scaleFactor = 1.0f; private HorizontalScrollView horizontalScrollView; private ScrollView scrollView; private RelativeLayout relativeLayout; @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_floor_plan); scrollView = findViewById(R.id.scrollView); relativeLayout = findViewById(R.id.relativeLayout); ImageView floorPlan = new ImageView(this); floorPlan.setImageResource(R.drawable.floorplan); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); floorPlan.setLayoutParams(layoutParams); relativeLayout.addView(floorPlan); scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener()); } public boolean dispatchTouchEvent(MotionEvent motionEvent) { super.dispatchTouchEvent(motionEvent); scaleGestureDetector.onTouchEvent(motionEvent); return true; } private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector scaleGestureDetector) { scaleFactor *= scaleGestureDetector.getScaleFactor(); scaleFactor = Math.max(1.0f, Math.min(scaleFactor, 4.0f)); scrollView.setScaleX(scaleFactor); scrollView.setScaleY(scaleFactor); return true; } } } 量词添加到上一个+数字匹配模式,并使用字符串锚点结束[0-9]关闭模式。

使用

$

<强>详情

  • '^\\\\([0-9]+\\\\) *[0-9]+ +[0-9]+$' - 字符串开头
  • ^ - 为MySQL命令翻译成\\\\( - 翻译成\\(字符串 - \(字符
  • ( - 1+位数
  • [0-9]+ - \\\\)字符
  • ) - 0+空格(使用 *匹配1个或更多)
  • + - 1+位,1 +位,1位数
  • [0-9]+ +[0-9]+ - 字符串结束。