Multiple commands

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce A. Julseth

    Multiple commands

    In my PHP code, I want to execute the following two commands:

    use mydb;
    select * from mytbl;

    Now, I can get it work with the following statements:

    $SQL = "use mydb";
    $result = mysql_query($SQ L );

    $SQL = "select * from mytbl";
    $result = mysql_query($SQ L );

    What I really wan to do is "something" like:

    $SQL = "use mydb ";
    $SQL .= "select * from mytbl;";

    $result = mysql_query($SQ L );

    i. e. I want to concatenate the two statement and execute the query only
    once.

    Is there a way to do this with mysql_query (or with someother function)?

    Thanks...


  • Pedro Graca

    #2
    Re: Multiple commands

    Bruce A. Julseth wrote:[color=blue]
    > $SQL = "use mydb ";
    > $SQL .= "select * from mytbl;";[/color]

    $SQL = 'select * from mydb.mytable';
    // no semicolon needed
    [color=blue]
    > $result = mysql_query($SQ L );
    >
    > i. e. I want to concatenate the two statement and execute the query only
    > once.
    >
    > Is there a way to do this with mysql_query (or with someother function)?[/color]

    Don't forget to test the mysql_query()

    $result = mysql_query($SQ L) or die('Oops! something wrong');

    --
    USENET would be a better place if everybody read: | to email me: use |
    http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
    http://www.netmeister.org/news/learn2quote2.html | header, textonly |
    http://www.expita.com/nomime.html | no attachments. |

    Comment

    • Michael Austin

      #3
      Re: Multiple commands

      Pedro Graca wrote:
      [color=blue]
      > Bruce A. Julseth wrote:
      >[color=green]
      >>$SQL = "use mydb ";
      >>$SQL .= "select * from mytbl;";[/color]
      >
      >
      > $SQL = 'select * from mydb.mytable';
      > // no semicolon needed
      >
      >[color=green]
      >> $result = mysql_query($SQ L );
      >>
      >>i. e. I want to concatenate the two statement and execute the query only
      >>once.
      >>
      >>Is there a way to do this with mysql_query (or with someother function)?[/color]
      >
      >
      > Don't forget to test the mysql_query()
      >
      > $result = mysql_query($SQ L) or die('Oops! something wrong');
      >[/color]
      $sql = $_POST['sqltxtinput'];
      $HOST = 'localhost'; // or hostname or ip address
      $USR = 'someusername';
      $PWD = 'somepassword';
      $DB = 'somedbname';

      $link=mysql_con nect($HOST,$USR ,$PWD,$DB)or die('Oops! something
      wrong'); if( $link ) {
      mysql_select_db ( $DB, $link );
      // clean up the sql from the input form
      if( $sql ) {
      $sql = ereg_replace( ";$", "", $sql );
      $sql = ereg_replace( "\\\\", "", $sql );
      // now execute the query.
      $result = mysql_query( $sql, $link );
      // Now process the data
      ...
      --
      Michael Austin.
      Consultant - Available.
      Donations welcomed. Http://www.firstdbasource.com/donations.html
      :)

      Comment

      • Tim Van Wassenhove

        #4
        Re: Multiple commands

        In article <40f5822f_2@new s1.prserv.net>, Bruce A. Julseth wrote:[color=blue]
        > What I really wan to do is "something" like:
        >
        > $SQL = "use mydb ";
        > $SQL .= "select * from mytbl;";
        >
        > $result = mysql_query($SQ L );[/color]

        I think more recent versions of MySQL support this. i
        But i don't it's enabled by default.

        Also keep in mind that this _can_ introduce security issues.

        --
        Tim Van Wassenhove <http://home.mysth.be/~timvw>

        Comment

        • Bruce A. Julseth

          #5
          Re: Multiple commands


          "Pedro Graca" <hexkid@hotpop. com> wrote in message
          news:slrncfb0gi .j2h.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
          > Bruce A. Julseth wrote:[color=green]
          > > $SQL = "use mydb ";
          > > $SQL .= "select * from mytbl;";[/color]
          >
          > $SQL = 'select * from mydb.mytable';
          > // no semicolon needed[/color]

          Thanks for this clean solution to the problem as I presented it. My
          apologies. I really wanted to string together several sql statements. A
          better example might have been;

          $SQL = "use mydb";
          $SQL .= "drop table if exists";
          $SQL .= "create table mytable (A "A attributes", B "B attributes",
          ........);

          Can this be done?

          Thanks....
          [color=blue]
          >[color=green]
          > > $result = mysql_query($SQ L );
          > >
          > > i. e. I want to concatenate the two statement and execute the query only
          > > once.
          > >
          > > Is there a way to do this with mysql_query (or with someother function)?[/color]
          >
          > Don't forget to test the mysql_query()
          >
          > $result = mysql_query($SQ L) or die('Oops! something wrong');
          >
          > --
          > USENET would be a better place if everybody read: | to email me: use |
          > http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
          > http://www.netmeister.org/news/learn2quote2.html | header, textonly |
          > http://www.expita.com/nomime.html | no attachments. |[/color]


          Comment

          • Bruce A. Julseth

            #6
            Re: Multiple commands


            "Tim Van Wassenhove" <euki@pi.be> wrote in message
            news:2llooiFdp4 bqU2@uni-berlin.de...[color=blue]
            > In article <40f5822f_2@new s1.prserv.net>, Bruce A. Julseth wrote:[color=green]
            > > What I really wan to do is "something" like:
            > >
            > > $SQL = "use mydb ";
            > > $SQL .= "select * from mytbl;";
            > >
            > > $result = mysql_query($SQ L );[/color]
            >
            > I think more recent versions of MySQL support this. i
            > But i don't it's enabled by default.[/color]

            Can anyone validate Tim's comment here? Does it exist in more recent
            versions of MySQL. If so, what versions and how is it turned on.

            Thanks...
            [color=blue]
            >
            > Also keep in mind that this _can_ introduce security issues.
            >
            > --
            > Tim Van Wassenhove <http://home.mysth.be/~timvw>[/color]


            Comment

            • Bruce A. Julseth

              #7
              Re: Multiple commands


              "Michael Austin" <maustin@firstd basource.com> wrote in message
              news:j3gJc.1521 9$L25.277@newss vr24.news.prodi gy.com...[color=blue]
              > Pedro Graca wrote:
              >[color=green]
              > > Bruce A. Julseth wrote:
              > >[color=darkred]
              > >>$SQL = "use mydb ";
              > >>$SQL .= "select * from mytbl;";[/color]
              > >
              > >
              > > $SQL = 'select * from mydb.mytable';
              > > // no semicolon needed
              > >
              > >[color=darkred]
              > >> $result = mysql_query($SQ L );
              > >>
              > >>i. e. I want to concatenate the two statement and execute the query only
              > >>once.
              > >>
              > >>Is there a way to do this with mysql_query (or with someother function)?[/color]
              > >
              > >
              > > Don't forget to test the mysql_query()
              > >
              > > $result = mysql_query($SQ L) or die('Oops! something wrong');
              > >[/color]
              > $sql = $_POST['sqltxtinput'];
              > $HOST = 'localhost'; // or hostname or ip address
              > $USR = 'someusername';
              > $PWD = 'somepassword';
              > $DB = 'somedbname';
              >
              > $link=mysql_con nect($HOST,$USR ,$PWD,$DB)or die('Oops! something
              > wrong'); if( $link ) {
              > mysql_select_db ( $DB, $link );
              > // clean up the sql from the input form
              > if( $sql ) {
              > $sql = ereg_replace( ";$", "", $sql );
              > $sql = ereg_replace( "\\\\", "", $sql );
              > // now execute the query.
              > $result = mysql_query( $sql, $link );
              > // Now process the data
              > ...[/color]

              I assumed by $sql = $_POST['sqltxtinput']; you meant input from a text
              area. So I created a page with a text area and entered the two statements
              in my example, namely

              use mydb
              select * from mytbl

              I couldn't get these two statement to work. If I only entered the "select"
              it worked fine.

              Did I do something wrong?

              Thanks....
              [color=blue]
              > --
              > Michael Austin.
              > Consultant - Available.
              > Donations welcomed. Http://www.firstdbasource.com/donations.html
              > :)[/color]


              Comment

              • Tim Van Wassenhove

                #8
                Re: Multiple commands

                In article <40f5cf0a_1@new s1.prserv.net>, Bruce A. Julseth wrote:[color=blue]
                >
                > "Tim Van Wassenhove" <euki@pi.be> wrote in message
                > news:2llooiFdp4 bqU2@uni-berlin.de...[color=green]
                >> In article <40f5822f_2@new s1.prserv.net>, Bruce A. Julseth wrote:[color=darkred]
                >> > What I really wan to do is "something" like:
                >> >
                >> > $SQL = "use mydb ";
                >> > $SQL .= "select * from mytbl;";
                >> >
                >> > $result = mysql_query($SQ L );[/color]
                >>
                >> I think more recent versions of MySQL support this. i
                >> But i don't it's enabled by default.[/color]
                >
                > Can anyone validate Tim's comment here? Does it exist in more recent
                > versions of MySQL. If so, what versions and how is it turned on.[/color]

                fe section 21.2.8 in the mysql manual.

                --
                Tim Van Wassenhove <http://home.mysth.be/~timvw>

                Comment

                • Tim Van Wassenhove

                  #9
                  Re: Multiple commands

                  In article <2lmhsiFe90eqU4 @uni-berlin.de>, Tim Van Wassenhove wrote:[color=blue]
                  > In article <40f5cf0a_1@new s1.prserv.net>, Bruce A. Julseth wrote:[color=green]
                  >> Can anyone validate Tim's comment here? Does it exist in more recent
                  >> versions of MySQL. If so, what versions and how is it turned on.[/color]
                  >
                  > fe section 21.2.8 in the mysql manual.[/color]

                  And i forgot to mention the mysqli documentation in the php manual.

                  --
                  Tim Van Wassenhove <http://home.mysth.be/~timvw>

                  Comment

                  • Michael Austin

                    #10
                    Re: Multiple commands

                    Tim Van Wassenhove wrote:
                    [color=blue]
                    > In article <2lmhsiFe90eqU4 @uni-berlin.de>, Tim Van Wassenhove wrote:
                    >[color=green]
                    >>In article <40f5cf0a_1@new s1.prserv.net>, Bruce A. Julseth wrote:
                    >>[color=darkred]
                    >>>Can anyone validate Tim's comment here? Does it exist in more recent
                    >>>versions of MySQL. If so, what versions and how is it turned on.[/color]
                    >>
                    >>fe section 21.2.8 in the mysql manual.[/color]
                    >
                    >
                    > And i forgot to mention the mysqli documentation in the php manual.
                    >[/color]
                    ... and the fact that it is MySQL 4.1 and later. (I am still at 4.0.15) Last
                    time I checked, AICBW, I believe 4.1 was not deemed "stable" yet.

                    --
                    Michael Austin.
                    Consultant - Available.
                    Donations welcomed. Http://www.firstdbasource.com/donations.html
                    :)

                    Comment

                    • Bruce A. Julseth

                      #11
                      Re: Multiple commands


                      "Michael Austin" <maustin@firstd basource.com> wrote in message
                      news:VMvJc.2390 $f96.606@newssv r22.news.prodig y.com...[color=blue]
                      > Tim Van Wassenhove wrote:
                      >[color=green]
                      > > In article <2lmhsiFe90eqU4 @uni-berlin.de>, Tim Van Wassenhove wrote:
                      > >[color=darkred]
                      > >>In article <40f5cf0a_1@new s1.prserv.net>, Bruce A. Julseth wrote:
                      > >>
                      > >>>Can anyone validate Tim's comment here? Does it exist in more recent
                      > >>>versions of MySQL. If so, what versions and how is it turned on.
                      > >>
                      > >>fe section 21.2.8 in the mysql manual.[/color]
                      > >
                      > >
                      > > And i forgot to mention the mysqli documentation in the php manual.
                      > >[/color]
                      > ... and the fact that it is MySQL 4.1 and later. (I am still at[/color]
                      4.0.15) Last[color=blue]
                      > time I checked, AICBW, I believe 4.1 was not deemed "stable" yet.[/color]

                      I'm at 4.0.14...
                      [color=blue]
                      >
                      > --
                      > Michael Austin.
                      > Consultant - Available.
                      > Donations welcomed. Http://www.firstdbasource.com/donations.html
                      > :)[/color]


                      Comment

                      Working...