PHP and MYSQL insert question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mkx
    Confirmed User
    • Nov 2003
    • 4001

    #1

    PHP and MYSQL insert question

    I have the following modem response being inserted into a field on a mysql database:

    This is the complete contents of the field:
    Code:
    AT+SSS="SHOW"
    
    +CT: 0,"OK","+974941949",,"09/12/15,03:06:18+00"
    Sup
    +CT: 1,"OK","+974941949",,"09/12/15,14:26:36+00"
    Yo
    +CT: 2,"OK","+974941949",,"09/12/15,22:30:51+00"
    Test
    OK
    I want to automatically reinsert certain parts of this field into a new table (called modem2 lets say) and divide them into 5 different fields

    FIELD NAMES =
    CT, STATUS, FROM, TIME, MESSAGE

    EXAMPLE FIELD INSERTIONS
    0, OK, +974941949, 09/12/15,03:06:18+00, Sup
    1, OK, +974941949, 09/12/15,14:26:36+00, Yo


    Anyone know how or where I can find out how to do this? Seems pretty simple if I only knew a bit more php
  • nicedreams
    Confirmed User
    • Apr 2004
    • 298

    #2
    Well, assuming you know how to split the fields up and know your mysql insert statement?

    You would use the mysql_query function with your mysql statement.


    http://us3.php.net/manual/en/function.mysql-connect.php

    http://us3.php.net/manual/en/function.mysql-query.php

    http://us3.php.net/manual/en/function.mysql-close.php

    Jimmy

    Nice Dreams Cash
    http://www.nicedreamscash.com

    Comment

    • Bird
      Confirmed User
      • Jan 2005
      • 4365

      #3
      What are you trying to do???


      Do you need to parse the output first or do you just want to build the SLQ insert funtion into the page.
      ICQ:268731675

      Comment

      • mkx
        Confirmed User
        • Nov 2003
        • 4001

        #4
        I just learned over the last hour how to split the fields. Now I have array [0] to array [19] outputted with the following script:

        Code:
        print_r (explode(",\"",$str));
        print_r (explode("\"",$str));
        echo "<br />";
        I now need to know how to declare say array[9] as $peanuts and echo it. From there I can figure out how to put it in the database. Tried code below but didn't work

        Code:
        print_r (explode(",\"",$str));
        print_r (explode("\"",$str));
        echo "<br />";
        
        $peanuts = $array[9];
        echo $peanuts;

        Comment

        • mkx
          Confirmed User
          • Nov 2003
          • 4001

          #5
          Originally posted by Bird
          What are you trying to do???


          Do you need to parse the output first or do you just want to build the SLQ insert funtion into the page.
          Just basically trying to take something from a field in my database that says hello, my, namell is ##fred and insert it back into a new table

          field1 field2 field3 field4 field5
          hello my name is fred

          Think i figured out the php explode / split thing, just need to get the correct results back in the database now.

          Comment

          • phypon
            Confirmed User
            • Oct 2003
            • 440

            #6
            loop through the array elements

            Comment

            • Bird
              Confirmed User
              • Jan 2005
              • 4365

              #7
              Oh, I see. You will get it...this is where we all start with php. You should look at W3schools or tiztag for tutorials.

              I wrote something real quick for your AT commands but not sure how exactly it would work for you. But should give you some Idea

              Code:
              <?php 
              $contents = file_get_contents("at.php");
              $arrays = explode('+CT:', $contents);
              print_r($arrays)."<hr />";
              //mysql_query($connect);
              
              	  
              foreach($arrays as $value){
              $i = 0;
              print_r($value)."<hr />";
              
              
                 for($i=0; $i < count($value); $i++) {
                   $new = explode(',',$value);
                       echo $new[1]. "|" .$new[2]."|".$new[3]."|".$new[4]."|".$new[5]."<br />";
                      //$sql = "INSERT INTO modem2 (CT,STATUS,FROM,TIME,MESSAGE) VALUES (' ',".$new[1].",".$new[2].","$new[3].")";
                    
              	  }
              
              }	  
              ?>
              Last edited by Bird; 12-15-2009, 08:29 PM.
              ICQ:268731675

              Comment

              • mkx
                Confirmed User
                • Nov 2003
                • 4001

                #8
                Originally posted by Bird
                Oh, I see. You will get it...this is where we all start with php. You should look at W3schools or tiztag for tutorials.

                I wrote something real quick for your AT commands but not sure how exactly it would work for you. But should give you some Idea

                Code:
                <?php 
                $contents = file_get_contents("at.php");
                $arrays = explode('+CT:', $contents);
                print_r($arrays)."<hr />";
                //mysql_query($connect);
                
                	  
                foreach($arrays as $value){
                $i = 0;
                print_r($value)."<hr />";
                
                
                   for($i=0; $i < count($value); $i++) {
                     $new = explode(',',$value);
                         echo $new[1]. "|" .$new[2]."|".$new[3]."|".$new[4]."|".$new[5]."<br />";
                        //$sql = "INSERT INTO modem2 (CT,STATUS,FROM,TIME,MESSAGE) VALUES (' ',".$new[1].",".$new[2].","$new[3].")";
                      
                	  }
                
                }	  
                ?>
                Thanks dude, working with that code now, will update in 10

                Comment

                • mkx
                  Confirmed User
                  • Nov 2003
                  • 4001

                  #9
                  hmm i got this to separate the thing into 15 arrays, 5 per message,

                  Code:
                  <?php
                  include 'dbconnect.php';
                  
                  $query  = "SELECT * FROM messages WHERE id=43";
                  $result = mysql_query($query);
                  
                  
                  while($row = mysql_fetch_array($result))
                    {
                    $str = $row['message'];
                  }
                  
                  $arrays = explode(',', $str);
                  print_r($arrays)."<hr />";
                  
                  //mysql_query($connect);
                  
                  	  
                  foreach($arrays as $value){
                  $i = 0;
                  print_r($value)."<hr />";
                  
                  
                     for($i=0; $i < count($value); $i++) {
                       $new = explode(',',$value);
                           echo $new[0]. "|" .$new[1]."|".$new[2]."|".$new[3]."|".$new[4]."|".$new[5]."<br />";
                  
                  
                  	  }
                  
                  }	  
                  ?>
                  doesn't seem to work with the insert command

                  Code:
                    $sql = "INSERT INTO modem2   ('zero','one','two','three','four','five') VALUES (".$new[0].",".$new[1].",".$new[2].",".$new[3].",".$new[4].","$new[5].")";
                  had to modify it a bit to get it working.

                  Comment

                  • mkx
                    Confirmed User
                    • Nov 2003
                    • 4001

                    #10
                    I think I got all the arrays declared properly, just need to know the insert command

                    Comment

                    • mkx
                      Confirmed User
                      • Nov 2003
                      • 4001

                      #11
                      Code:
                      <?php
                      include 'dbconnect.php';
                      
                      
                      
                      $query  = "SELECT * FROM messages WHERE id=43";
                      $result = mysql_query($query);
                      
                      
                      while($row = mysql_fetch_array($result))
                        {
                        $str = $row['message'];
                        }
                      
                      
                      print_r (explode(",\"",$str));
                      print_r (explode("\"",$str));
                      echo "<br />";
                      
                      ?>
                      This code does the splits just fine, splits it into 20 arrays as it does it twice. I just need to say now that array[11] = $peanuts and i will be good to go

                      Comment

                      • Bird
                        Confirmed User
                        • Jan 2005
                        • 4365

                        #12
                        Don't forget the ID key value for the tables next increment...if tour db reads

                        1 ok name number from to message
                        2 ok name number from to message
                        3 ok name number from to message
                        ...

                        88 ok name number from to message
                        89 ok name number from to message

                        then the next input would fill automatically.

                        INSERT INTO modem2 ('id','zero','one') VALUES ('DELETETHIS', ".$new[0].",".$new[1].")";

                        see how the first value is blank, it would automatically populate id's next entry

                        90 ok name number from to message
                        91 ok name number from to message
                        Last edited by Bird; 12-15-2009, 09:08 PM.
                        ICQ:268731675

                        Comment

                        • Killswitch - BANNED FOR LIFE

                          #13
                          Originally posted by mkx
                          ...Seems pretty simple if I only knew a bit more php
                          I love when newbies or people who don't know shit about PHP think, oh it's simple and should be easy for anyone!

                          "I need Google recreated, you're pretty good, should be simple and easy for you, how long you think it will take? 10 mins?"

                          Makes me

                          * PS: this wasn't directed at you, I just hear that sentence all the time.

                          Comment

                          • Bird
                            Confirmed User
                            • Jan 2005
                            • 4365

                            #14
                            Originally posted by Killswitch
                            I love when newbies or people who don't know shit about PHP think, oh it's simple and should be easy for anyone!

                            "I need Google recreated, you're pretty good, should be simple and easy for you, how long you think it will take? 10 mins?"

                            Makes me

                            * PS: this wasn't directed at you, I just hear that sentence all the time.
                            I'm cool with people wanting to get started with php its those businesses that think "hey can you make me a myspace type site".
                            ICQ:268731675

                            Comment

                            • mkx
                              Confirmed User
                              • Nov 2003
                              • 4001

                              #15
                              Originally posted by Bird
                              Don't forget the ID key value for the tables next increment...if tour db reads

                              1 ok name number from to message
                              2 ok name number from to message
                              3 ok name number from to message
                              ...

                              88 ok name number from to message
                              89 ok name number from to message

                              then the next input would fill automatically.

                              INSERT INTO modem2 ('id','zero','one') VALUES ('DELETETHIS', ".$new[0].",".$new[1].")";

                              see how the first value is blank, it would automatically populate id's next entry

                              90 ok name number from to message
                              91 ok name number from to message

                              hmm still having problems. how do I just echo a certain array. I got it to output:
                              Code:
                              Array ( [0] => 0 [1] => OK" [2] => +974941949", [3] => 09/12/15,03:06:18+00
                              So say I wanted to echo array 2 (+974941949), how would I echo it? I tried several variations such as
                              echo $array(2);
                              echo $array[2];
                              $array[2] = $hello; echo $hello;

                              etc, just cant seem to figure it out

                              Comment

                              • Killswitch - BANNED FOR LIFE

                                #16
                                echo $array[2];

                                Comment

                                • Bird
                                  Confirmed User
                                  • Jan 2005
                                  • 4365

                                  #17
                                  Originally posted by mkx
                                  hmm still having problems. how do I just echo a certain array. I got it to output:
                                  Code:
                                  Array ( [0] => 0 [1] => OK" [2] => +974941949", [3] => 09/12/15,03:06:18+00
                                  So say I wanted to echo array 2 (+974941949), how would I echo it? I tried several variations such as
                                  echo $array(2);
                                  echo $array[2];
                                  $array[2] = $hello; echo $hello;

                                  etc, just cant seem to figure it out


                                  Replace

                                  $arrays with $arrays[3]

                                  Code:
                                  $contents = file_get_contents("at.php");
                                  $arrays = explode('+CT:', $contents);
                                  //print_r($arrays);
                                  print_r($arrays[3]);
                                  ICQ:268731675

                                  Comment

                                  • Bird
                                    Confirmed User
                                    • Jan 2005
                                    • 4365

                                    #18
                                    Originally posted by Killswitch
                                    echo $array[2];
                                    In that little piece of code I wrote it says $array's' not $array, don;t forget the "S"
                                    ICQ:268731675

                                    Comment

                                    • Killswitch - BANNED FOR LIFE

                                      #19
                                      Originally posted by Bird
                                      In that little piece of code I wrote it says $array's' not $array, don;t forget the "S"
                                      Didn't look at the code, just went by what he asked. How to echo an array.

                                      Comment

                                      • Bird
                                        Confirmed User
                                        • Jan 2005
                                        • 4365

                                        #20
                                        Originally posted by Killswitch
                                        Didn't look at the code, just went by what he asked. How to echo an array.
                                        I know, It was just a note
                                        ICQ:268731675

                                        Comment

                                        • Bird
                                          Confirmed User
                                          • Jan 2005
                                          • 4365

                                          #21
                                          Dude if you want a free text messaging solution, I have one for you...

                                          http://mobiquio.com/playsms

                                          login:demo and pass:password
                                          ICQ:268731675

                                          Comment

                                          • mkx
                                            Confirmed User
                                            • Nov 2003
                                            • 4001

                                            #22
                                            I tried with the 'S" and it still won't print the result

                                            Code:
                                            <?php
                                            include 'dbconnect.php';
                                            
                                            
                                            
                                            $query  = "SELECT * FROM messages WHERE id=43";
                                            $result = mysql_query($query);
                                            
                                            
                                            while($row = mysql_fetch_array($result))
                                              {
                                              $str = $row['message'];
                                              }
                                            
                                            
                                            print_r (explode(",\"",$str));
                                            print_r (explode("\"",$str));
                                            
                                            
                                            
                                            [B]echo $arrays[3];
                                            echo $arrays[4];
                                            print_r($arrays[5]);
                                            [/B]
                                            
                                            ?>

                                            Comment

                                            • mkx
                                              Confirmed User
                                              • Nov 2003
                                              • 4001

                                              #23
                                              Originally posted by Bird
                                              Dude if you want a free text messaging solution, I have one for you...

                                              http://mobiquio.com/playsms

                                              login:demo and pass:password
                                              It's a bit more complicated

                                              Comment

                                              • mkx
                                                Confirmed User
                                                • Nov 2003
                                                • 4001

                                                #24
                                                might have got it, trying it out now

                                                Comment

                                                • mkx
                                                  Confirmed User
                                                  • Nov 2003
                                                  • 4001

                                                  #25
                                                  Yup got it! Just changed to:

                                                  $arrays = (explode(",\"",$str));
                                                  $arrays = (explode("\"",$str));

                                                  Thanks a bunch everyone

                                                  Comment

                                                  • potter
                                                    Confirmed User
                                                    • Dec 2004
                                                    • 6559

                                                    #26
                                                    Man, this thread should have been over in like 1-2 replies. Talk about your basic basic programming function, It's so sad it took 25.

                                                    I guess that's what you get on a webmaster board.

                                                    Comment

                                                    • gandalfuy
                                                      Confirmed User
                                                      • Jun 2005
                                                      • 4686

                                                      #27
                                                      Originally posted by potter
                                                      Man, this thread should have been over in like 1-2 replies. Talk about your basic basic programming function, It's so sad it took 25.

                                                      I guess that's what you get on a webmaster board.
                                                      He got his answer though.
                                                      I'm not back.

                                                      Comment

                                                      • mkx
                                                        Confirmed User
                                                        • Nov 2003
                                                        • 4001

                                                        #28
                                                        hey bird, do you have icq? might have some paid work for you

                                                        Comment

                                                        • Bird
                                                          Confirmed User
                                                          • Jan 2005
                                                          • 4365

                                                          #29
                                                          Originally posted by mkx
                                                          hey bird, do you have icq? might have some paid work for you
                                                          268731675 just added you
                                                          ICQ:268731675

                                                          Comment

                                                          Working...