2

I have a sql file which is having lot of insert queries. is there any way to convert these insert queries to php arrays?

i dont want to insert these items directly to database. before inserting i want to filter lot of items from these

`INSERT INTO `products` (`p_id`, `p_name`, `p_parent`, `p_dependent`,`p_pid`) VALUES
(1, 'some', "some", '2', '1'),(1, 'some', "some", '2', '1'),(1, 'some', "some", '2', '1'),(1, 'some', "some", '2', '1')

is there any option in regex to convert these into array?

0

2 Answers 2

2

Take the whole query in a PHP string, then explode it for paranthesis and you will find (after the first return) your query values. From here, you can further explode them by commas to return each value of the query if you want to avoid some of them. After having your queries all good in a PHP array, just parse the array and execute insert statements with the values in your array.

Sign up to request clarification or add additional context in comments.

Comments

0

you can use this function

function convert_QueryString_to_array($QueryString) {
    //---get fields-----
    $pos_fields_start=strpos($QueryString,'(')+1;
    $pos_fields_end=strpos($QueryString,')');
    $length=$pos_fields_end-$pos_fields_start;
    $insert=substr($QueryString,$pos_fields_start,$length);
    $fields=explode(",", $insert);

    //---get values----
    $Values=substr($QueryString,$pos_fields_end+1);
    $pos_value_start=strpos($Values,'(')+1;
    $pos_value_end=strpos($Values,')');
    $length=$pos_value_end-$pos_value_start;
    $value=substr($Values,$pos_value_start,$length);
    $fields_value=explode(",", $value);

    //---result------
    $result_array=array();
    for ($i=0;$i<count($fields);$i++ {
        $result_array[$fields[$i]]=$fields_value[$i];
    }
    return $result_array;
}

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Please do not post "do this" answers on Stack Overflow. Your answer will be much more helpful to the asker and future researchers if you include a plain English explanation of how your snippet works and why you feel it is the best approach. What if a table name, column name, or value contains a parenthesis? This doesn't strike me as a robust SQL parser.

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.