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

    Working...