Log in

View Full Version : Splitting array twice



motormichael12
07-03-2008, 04:43 AM
I am making a script where a user can execute mysql queries, but I need it to split the queries by the semicolon and also find the type of query it is. I have this code at the moment:


if ($query)
{
$these = explode(";", $query);

for($i=0;$i<count($these);$i++)
{
$splitagain = explode(" ", trim($these[$i]));
for($x=0;$x<count($these);$x++)
{
echo "i: " . $i . "<br>";
echo "x: " . $x . "<br>";
$query_type = strtoupper($splitagain[x]);
echo "splitagain[x]: " . $splitagain[$x] . "<br>";
echo "these[x]: " . $these[$x] . "<br><br>";

switch ($query_type){
case 'SHOW':
case 'SELECT':
$this->query_display($query);
break;
case 'UPDATE':
$this->query_background($query);
break;
}
}
}
}


Now lets say I enter in the following query:

select * from config; update table set er = 9 where er = 3
The output:

i: 0
x: 0
splitagain[x]: select
these[x]: select * from config

i: 0
x: 1
splitagain[x]: *
these[x]: update table set er = 9 where er = 3

i: 1
x: 0
splitagain[x]: update
these[x]: select * from config

i: 1
x: 1
splitagain[x]: table
these[x]: update table set er = 9 where er = 3

I only need it to produce one output, so that for this query it would be like:


splitagain[x]: select
these[x]: select * from config

splitagain[x]: update
these[x]: update table set er = 9 where er = 3

Can anyone help me with this?

techietim
07-03-2008, 12:10 PM
Would this be better for you?


<?php
$query = 'select * from config; update table set er = 9 where er = 3';
$query = explode(';', $query);
$query = array_map('trim', $query);
foreach($query as $subQuery){
$queryType = substr($subQuery, 0, strpos($subQuery, ' '));
$queryType = strtolower($queryType);
switch($queryType){
case 'SHOW':
case 'SELECT':
$this->query_display($subQuery);
break;
case 'UPDATE':
$this->query_background($subQuery);
break;
}
}

motormichael12
07-03-2008, 12:18 PM
The query is submitted through a text box so I modified that to accept the text through the box, but it did not return anything. I have it so the value of the query will show up again in the text box and it returned "Array"

I believe I know the problem:


$queryType = substr($subQuery, 0, strpos($subQuery, ' '));

PHP.net example of strstr:

<?php
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com

$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user; // prints name
?>


I am using php version 4.4.4

motormichael12
07-03-2008, 12:32 PM
Got it :D
Since I only needed the first word of each one, I realized I didn't need teh second for() loop. Now the top looks likethis:


$these = explode(";", $query);

for($i=0;$i<count($these);$i++)
{
$splitagain = explode(" ", trim($these[$i]));

echo "i: " . $i . "<br>";
echo "x: " . $x . "<br>";
$query_type = strtoupper($splitagain[x]);
echo "splitagain[0]: " . $splitagain[0] . "<br>";
echo "these[i]: " . $these[$i] . "<br><br>";


Returns:

i: 0
x:
splitagain[0]: select
these[i]: select * from phpbb_config

i: 1
x:
splitagain[0]: update
these[i]: update ab set er = 9 where er = 3

NOw I know I need $these[$i] for the query and $splitagain[$x] to decide where to send $these[$i]

Thanks for trying to help though! :D