-1

what i need

   select column1,column2,column3 from table where  condition

output should be

select column1 from table where condition

i try to use explode

     print_r (explode(",",$str));

Output i need

Array

[0] => SELECT column1
[1] => from 
[2] => table

and so on .

but it will split into array and sql is dyanmic .

  • any suggestion is most welcome.
8
  • So your explode() ignores the first space, seems to work on commas as well and converts part of you string to upper-case. Curious. Commented Mar 21, 2018 at 13:02
  • Cannot reproduce Commented Mar 21, 2018 at 13:05
  • i need sql select column1 from table where condition Commented Mar 21, 2018 at 13:08
  • This will take more than just using explode(). Have you tried anything else? Commented Mar 21, 2018 at 13:08
  • can php gives input select column1,column2,column3 from table where condition Commented Mar 21, 2018 at 13:13

2 Answers 2

1

Very ugly solution :

$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;
Sign up to request clarification or add additional context in comments.

2 Comments

sql column is dynamic it may increase/decrease
well it dont care of the number of the column here, it just takes the first element (in you example : column1)
1

If the performance is not a problem you could try something like:

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

preg_match('/select (.*) from/i', $query, $matches);
$columns = explode(',', $matches[1]);
var_dump($columns);

http://sandbox.onlinephpfunctions.com/code/4a3ea6230b8efd49f0d99288756cef4fc378975f

2 Comments

i need output sql query select column1 from table where condition
@afeef I didn't undertand you. If you can try to put the expected result :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.