preg_match help

Discussion in 'PHP' started by php-lover, Feb 12, 2010.

  1. #1
    Hi guys, I'm wondering if there's any regx ninja that can solve my problem.

    
    
    1.QUERY.
    
    $query = 'UPDATE tbl_class SET class_status = 3 WHERE class_id = 4 AND class_status = 2';
    
    From the above query, I want to grab only the "class_status = 2" part and also the "class_id = 4" part.
    
    2.QUERY
    $query = 'INSERT INTO tbl_stud_class(class_id, stud_id) VALUES(1, 3)';
    
    From the above query, I want to grab only the 'class_id' and 'class_id values 1' part, I also want to grab the 'stud_id' and 'stud_id values 3' part
    
    To be more clear. I want the result in a array like 
    $match = array('class_id' => 1, 'stud_id' => 3);
     
     
    PHP:
    Your help will appriciated.
    Thanks.
     
    php-lover, Feb 12, 2010 IP
  2. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I like to help you, but to tell true I cant understand what you are trying for.

    You are updating and inserting data to table and you say you need to grab certain details in an array ..

    that is not possible ... Are you trying to execute a query which will " select " the details you need as in array ?
     
    HivelocityDD, Feb 12, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi,
    Can you be a little bit specific: I mean if you provide tables structure (as create table scripts; no pictures please :) ) with few lines data in them an desired result set, I'm almost sure you don't need regexp here.
    Regards
    p.p.: How to grab data via Insert?
     
    koko5, Feb 12, 2010 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Hi
    I'm not trying to insert something to the database.
    I want to use the preg_match (regex pattern) to grab a parts of string from a query.
     
    php-lover, Feb 12, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    ok, maybe this:

    
    $s='INSERT INTO tbl_stud_class(class_id, stud_id) VALUES(1, 3)';
    $arr=array();
    preg_match_all('#\(([^\),]+),([^\),]+)\)#ius',$s,&$arr);
    //print_r($arr); you can uncomment
    if (count($arr[1])) echo implode(' ',$arr[1])."\r\n";
    if (count($arr[2])) echo implode(' ',$arr[2])."\r\n";
    
    PHP:
     
    koko5, Feb 12, 2010 IP
  6. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    that's brilliant. It's works

    Cheers mate :)
     
    php-lover, Feb 12, 2010 IP