Multiple queries in one script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Sanderson

    Multiple queries in one script

    I am relatively new to PHP and MySQL. This is the first time I've tried
    to use multiple queries in a single script.

    I have the following PHP script which gets a Job Number from a search
    form and generates a web page which displays the record for that job:

    $username="root ";
    $password="";
    $database="foob ar";

    $Searchterm = $_GET['JobNumber'];

    mysql_connect(' localhost',$use rname,$password );
    @mysql_select_d b($database) or die( "Unable to select database");

    $query="select * from jobs where JobNumber like $Searchterm";

    $result=mysql_q uery($query);
    $num_results=my sql_num_rows($r esult);
    $JobNumber=mysq l_result($resul t,$i,"JobNumber ");
    $NSN=mysql_resu lt($result,$i," NSN");

    echo various fields here

    This part works fine.

    ----------------------------------

    I now want to list other jobs which use the same NSN on the same page
    under that display. This is the script I'm using:

    $query2="select * from jobs where NSN like $NSN";
    $result=mysql_q uery($query2);
    $num_results=my sql_num_rows($r esult);
    $JobNumber=mysq l_result($resul t,$i,"JobNumber ");

    echo query2; (returns the correct query, including the NSN)
    echo $num_results (returns nothing)

    $i=0;
    while ($i < $num) {

    echo various fields here (returns nothing)

    $i++;
    }

    Can anyone tell me what I'm missing?




  • Erwin Moller

    #2
    Re: Multiple queries in one script

    Bob Sanderson wrote:

    Hi Bob,
    [color=blue]
    > I am relatively new to PHP and MySQL. This is the first time I've tried
    > to use multiple queries in a single script.
    >
    > I have the following PHP script which gets a Job Number from a search
    > form and generates a web page which displays the record for that job:
    >
    > $username="root ";
    > $password="";
    > $database="foob ar";
    >
    > $Searchterm = $_GET['JobNumber'];
    >
    > mysql_connect(' localhost',$use rname,$password );
    > @mysql_select_d b($database) or die( "Unable to select database");
    >[/color]

    Ok so far.
    [color=blue]
    > $query="select * from jobs where JobNumber like $Searchterm";[/color]

    This is very dangerous.
    NEVER EVER thrust input originating from a form that is filled in by some
    user.
    You are wide open to the SQL-Injection attack this way.

    If you have magic_quotes on, you are a lot safer, but please be sure what
    you are doing...
    [color=blue]
    >
    > $result=mysql_q uery($query);
    > $num_results=my sql_num_rows($r esult);
    > $JobNumber=mysq l_result($resul t,$i,"JobNumber ");
    > $NSN=mysql_resu lt($result,$i," NSN");[/color]

    What is $i here?
    $i defines the row to be retrieved, but you didn't give it any value.
    [color=blue]
    >
    > echo various fields here
    >
    > This part works fine.[/color]

    good. :-)

    Suprisingly because you didn't define $i.....
    [color=blue]
    >
    > ----------------------------------
    >
    > I now want to list other jobs which use the same NSN on the same page
    > under that display. This is the script I'm using:
    >
    > $query2="select * from jobs where NSN like $NSN";
    > $result=mysql_q uery($query2);
    > $num_results=my sql_num_rows($r esult);
    > $JobNumber=mysq l_result($resul t,$i,"JobNumber ");[/color]

    What is $i here?
    [color=blue]
    >
    > echo query2; (returns the correct query, including the NSN)[/color]

    That should be:
    echo $query2;

    You forgot the $
    [color=blue]
    > echo $num_results (returns nothing)[/color]

    should return something if you fix the previous code. :-)
    [color=blue]
    >
    > $i=0;
    > while ($i < $num) {[/color]

    What is $num?
    Do you mean $num_results???
    [color=blue]
    >
    > echo various fields here (returns nothing)
    >
    > $i++;
    > }
    >
    > Can anyone tell me what I'm missing?[/color]

    Fix the various mistakes. :-)
    Good luck!

    Regards,
    Erwin Moller

    Comment

    • Bob Sanderson

      #3
      Re: Multiple queries in one script

      Sorry, there's a couple of typos there (in the message, not in the script).
      Here's what the actual script reads, not including the for-next loop:

      $query2="select * from jobs where NSN like $NSN";
      $result=mysql_q uery($query2);
      $num_results=my sql_num_rows($r esult);

      $NSN=mysql_resu lt($result,$i," NSN");

      echo "query = $query2<br>";
      echo "num_result s = $num_results<br >";

      Again, the query is exactly what I want, but $num_results returns nothing
      (I'm sure that there are more than 1 records meeting the criteria).

      < What is $i here?

      As I said, I'm new to MySql. I took an example from a book and the $i was
      included. It appears that, in this case, it's unnecessary but it doesn't
      seem to matter if it's there or not.

      Comment

      • Bob Sanderson

        #4
        Re: Multiple queries in one script

        Sorry, there's a couple of typos there (in the message, not in the script).
        Here's what the actual script reads, not including the for-next loop:

        $query2="select * from jobs where NSN like $NSN";
        $result=mysql_q uery($query2);
        $num_results=my sql_num_rows($r esult);

        $NSN=mysql_resu lt($result,$i," NSN");

        echo "query = $query2<br>";
        echo "num_result s = $num_results<br >";

        Again, the query is exactly what I want, but $num_results returns nothing
        (I'm sure that there are more than 1 records meeting the criteria).

        < What is $i here?

        As I said, I'm new to MySql. I took an example from a book and the $i was
        included. It appears that, in this case, it's unnecessary but it doesn't
        seem to matter if it's there or not.

        Comment

        • Bob Sanderson

          #5
          Re: Multiple queries in one script

          Sorry, there's a couple of typos there (in the message, not in the script).
          Here's what the actual script reads, not including the for-next loop:

          $query2="select * from jobs where NSN like $NSN";
          $result=mysql_q uery($query2);
          $num_results=my sql_num_rows($r esult);

          $NSN=mysql_resu lt($result,$i," NSN");

          echo "query = $query2<br>";
          echo "num_result s = $num_results<br >";

          Again, the query is exactly what I want, but $num_results returns nothing
          (I'm sure that there are more than 1 records meeting the criteria).

          < What is $i here?

          As I said, I'm new to MySql. I took an example from a book and the $i was
          included. It appears that, in this case, it's unnecessary but it doesn't
          seem to matter if it's there or not.

          Comment

          • Bob Sanderson

            #6
            Re: Multiple queries in one script

            Forgive me if this is posted more than once, but it is not showing up in my
            news reader.

            ===============

            Sorry, there's a couple of typos there (in the message, not in the script).
            Here's what the actual script reads, not including the for-next loop:

            $query2="select * from jobs where NSN like $NSN";
            $result=mysql_q uery($query2);
            $num_results=my sql_num_rows($r esult);

            $NSN=mysql_resu lt($result,$i," NSN");

            echo "query = $query2<br>";
            echo "num_result s = $num_results<br >";

            Again, the query is exactly what I want, but $num_results returns nothing
            (I'm sure that there are more than 1 records meeting the criteria).

            < What is $i here?

            As I said, I'm new to MySql. I took an example from a book and the $i was
            included. It appears that, in this case, it's unnecessary but it doesn't
            seem to matter if it's there or not.

            Comment

            • deja@chronofish.com

              #7
              Re: Multiple queries in one script

              Hi Bob,

              What you are missing are quotes. Your second query should read:

              $query2="select * from jobs where NSN like '$NSN' ";

              My recommendation is to print the query out when you debugging your
              code - and then do a copy/paste into something like phpMyAdmin or Mysql
              Command Center or at the Mysql command line - basically test the query
              out in an app that will let you make quick changes and retest. When
              you are able to get the query working correctly, then copy it back into
              your code and (replacing vars as needed).

              -CF

              Comment

              • deja@chronofish.com

                #8
                Re: Multiple queries in one script

                Hi Bob,

                If $i is not perviously set, then it will equal "". It should equal an
                integer value of 0 or greater. This probably belongs on the PHP
                newgroup, but the PHP/Mysql community tends to be pretty tight.

                mysql_result is a function that is defined as:
                mysql_result ( resource result, int row [, mixed field] )

                A VERY handy resource (one of the best programmer API documentation
                emplemetation I have come across) is PHP.net. Specificially see this
                link:

                PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


                -CF

                Comment

                • Bob Sanderson

                  #9
                  Re: Multiple queries in one script

                  Sorry, there's a couple of typos there (in the message, not in the script).
                  Here's what the actual script reads, not including the for-next loop:

                  $query2="select * from jobs where NSN like $NSN";
                  $result=mysql_q uery($query2);
                  $num_results=my sql_num_rows($r esult);

                  $NSN=mysql_resu lt($result,$i," NSN");

                  echo "query = $query2<br>";
                  echo "num_result s = $num_results<br >";

                  Again, the query is exactly what I want, but $num_results returns nothing
                  (I'm sure that there are more than 1 records meeting the criteria).

                  < What is $i here?

                  As I said, I'm new to MySql. I took an example from a book and the $i was
                  included. It appears that, in this case, it's unnecessary but it doesn't
                  seem to matter if it's there or not.

                  Comment

                  Working...