Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中解析sql查询_Php - Fatal编程技术网

如何在php中解析sql查询

如何在php中解析sql查询,php,Php,我需要什么 select column1,column2,column3 from table where condition 输出应该是 从条件所在的表中选择column1 我尝试使用explode print_r (explode(",",$str)); 我需要的输出 排列 等等 但它将被拆分为数组,sql是动态的 任何建议都欢迎 如果性能不是问题,您可以尝试以下方法: <?php $query = 'select column1,column2,column3

我需要什么

   select column1,column2,column3 from table where  condition
输出应该是

从条件所在的表中选择column1

我尝试使用explode

     print_r (explode(",",$str));
我需要的输出

排列

等等

但它将被拆分为数组,sql是动态的

  • 任何建议都欢迎

如果性能不是问题,您可以尝试以下方法:

<?php
$query = 'select column1,column2,column3 from table where  condition';

preg_match('/select (.*) from/i', $query, $matches);
$columns = explode(',', $matches[1]);
var_dump($columns);
非常丑陋的解决方案:

$string = "select column1,column2,column3 from table where  condition";

$data1 = explode(" ",$string);
$data2 = explode(",",$data1[1]);
$data1[1] = $data2[0];
$string2 = implode($data1, " ");

print $string2;

因此,您的
explode()
忽略第一个空格,似乎也处理逗号,并将字符串的一部分转换为大写。好奇。我需要sql从表中选择column1,条件是这不仅仅需要使用
explode()
。您还试过其他方法吗?php可以提供输入从表where条件中选择column1、column2、column3吗我需要输出sql查询从表where中选择column1吗condition@afeef我没有理解你。如果您可以尝试放置预期结果:)sql列是动态的,它可能会增加/减少它不关心这里的列数,它只需要第一个元素(在您的示例中:column1)
$string = "select column1,column2,column3 from table where  condition";

$data1 = explode(" ",$string);
$data2 = explode(",",$data1[1]);
$data1[1] = $data2[0];
$string2 = implode($data1, " ");

print $string2;