Results 1 to 4 of 4

Thread: Splitting array twice

  1. #1
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default Splitting array twice

    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:

    PHP Code:
                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:
    Code:
    select * from config; update table set er = 9 where er = 3
    The output:
    Code:
    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:
    Code:
    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?

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Would this be better for you?
    PHP Code:
    <?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($subQuery0strpos($subQuery' '));
      
    $queryType strtolower($queryType);
      switch(
    $queryType){
        case 
    'SHOW':
        case 
    'SELECT':
          
    $this->query_display($subQuery);
        break;
        case 
    'UPDATE':
          
    $this->query_background($subQuery);
        break;
      }
    }

  3. #3
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    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:

    Code:
      $queryType = substr($subQuery, 0, strpos($subQuery, ' '));
    PHP.net example of strstr:
    Code:
    <?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

  4. #4
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Got it
    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:

    PHP Code:
                    $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:
    Code:
    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •