Variable in select row

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Garry Jones

    Variable in select row

    I want to Select a field based upon the number the user keys in I want to
    retrieve another field from the same record

    This is not working. How do I do it? The tricky bit seems to me to be
    putting the variavble in the select quotes.

    $scfchknum=$_PO ST['scfchknum'];
    (inlogging mysql)
    $upgcollect='SE LECT scfpwd1 FROM scfmforening WHERE scfmnum = $scfchknum';
    (logout mysql)
    $pwd1=mysql_que ry($upgcollect) ;
    mysql_close();
    echo $pwd1;

    $scfchknum is the value of the variable assign to user input
    scfmforening is the name of the table
    scfmnum is the name of the field I want to search in
    scfpwd1 is name of the field I want to retreive from
    $upgcollect is the data before it is processed with query
    $pwd1 is the data after it is processed.

    Greatful for any help.
    Garry Jones
    Sweden


  • Mateusz Markowski

    #2
    Re: Variable in select row


    Garry Jones napisal(a):
    $upgcollect='SE LECT scfpwd1 FROM scfmforening WHERE scfmnum = $scfchknum';
    After it $upgcollect containts such string: "SELECT scfpwd1 FROM
    scfmforening WHERE scfmnum = $scfchknum". $scfchknum was not changed on
    its value. You should have written:

    $upgcollect="SE LECT scfpwd1 FROM scfmforening WHERE scfmnum =
    $scfchknum";
    or
    $upgcollect='SE LECT scfpwd1 FROM scfmforening WHERE scfmnum =
    '.$scfchknum;

    Comment

    • Garry Jones

      #3
      Re: Variable in select row

      "Mateusz Markowski" <mateusz@bsdmai l.orgskrev i meddelandet
      news:1160322153 .961306.179050@ k70g2000cwa.goo glegroups.com.. .
      You should have written:
      $upgcollect="SE LECT scfpwd1 FROM scfmforening WHERE scfmnum =
      $scfchknum";
      No, I still can not get this working.

      The examples on the net are complex and I can not deduce the simple line of
      code that I need from them

      This is the problem. Put simply

      $var1=$_POST['anum'];
      (inlogging mysql)
      $var2="SELECT field2 FROM table1 WHERE field4 = $var1";
      (logout mysql)
      mysql_close();

      (Need help with necessary code to assign variable $var2 to the value of
      field4 for the record where users value is found)

      I can not use the row index as that is assigned to another sequential value
      and unknown to the user.

      For instance

      A table (table1) with the following fields

      index, field2, town, townnum

      Records of
      1 abc dallas 5
      2 def denver 3
      3 ghh atlanta 17
      4 gfe gateshead 24

      The user knows the townnumber (5 or 8 or 17 or 24). The user does not know
      the index, but townnum will always have a unique value.

      He keys in his number.

      I then want to look for the value of field 2 in the record where the user's
      townnumber was found.

      I then want to assign a variable to the value of the field2 in that record

      If he types in 3 my code should assign the value of def to $var2

      I understand its a combination of select, query and result but need help
      with the syntax.

      Any help greatly appreciated.

      Garry Jones
      Sweden







      Comment

      • Jerry Stuckle

        #4
        Re: Variable in select row

        Garry Jones wrote:
        "Mateusz Markowski" <mateusz@bsdmai l.orgskrev i meddelandet
        news:1160322153 .961306.179050@ k70g2000cwa.goo glegroups.com.. .
        >
        >
        >>You should have written:
        >
        >
        >>$upgcollect=" SELECT scfpwd1 FROM scfmforening WHERE scfmnum =
        >>$scfchknum" ;
        >
        >
        No, I still can not get this working.
        >
        The examples on the net are complex and I can not deduce the simple line of
        code that I need from them
        >
        This is the problem. Put simply
        >
        $var1=$_POST['anum'];
        (inlogging mysql)
        $var2="SELECT field2 FROM table1 WHERE field4 = $var1";
        (logout mysql)
        mysql_close();
        >
        (Need help with necessary code to assign variable $var2 to the value of
        field4 for the record where users value is found)
        >
        I can not use the row index as that is assigned to another sequential value
        and unknown to the user.
        >
        For instance
        >
        A table (table1) with the following fields
        >
        index, field2, town, townnum
        >
        Records of
        1 abc dallas 5
        2 def denver 3
        3 ghh atlanta 17
        4 gfe gateshead 24
        >
        The user knows the townnumber (5 or 8 or 17 or 24). The user does not know
        the index, but townnum will always have a unique value.
        >
        He keys in his number.
        >
        I then want to look for the value of field 2 in the record where the user's
        townnumber was found.
        >
        I then want to assign a variable to the value of the field2 in that record
        >
        If he types in 3 my code should assign the value of def to $var2
        >
        I understand its a combination of select, query and result but need help
        with the syntax.
        >
        Any help greatly appreciated.
        >
        Garry Jones
        Sweden
        >
        >
        >
        >
        >
        >
        >
        Garry,

        $var2 is just a string. You have made no call to MySQL.

        Once you have the SQL statement, you must pass it to mysql_query() to
        query the database. Then check the result (which is a result set, not
        the data itself); false indicates an error.

        If there is no error, you must call mysql_fetch_arr ay() or
        mysql_fetch_res ult() to retrieve the actual data.

        See the PHP manual for more information.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Pedro Graca

          #5
          Re: Variable in select row

          Garry Jones wrote:
          A table (table1) with the following fields
          >
          index, field2, town, townnum
          >
          Records of
          1 abc dallas 5
          2 def denver 3
          3 ghh atlanta 17
          4 gfe gateshead 24
          >
          The user knows the townnumber (5 or 8 or 17 or 24). The user does not know
          the index, but townnum will always have a unique value.
          >
          He keys in his number.
          Let's say it's in a POST form. So his number will be $_POST['townnum']
          I then want to look for the value of field 2 in the record where the user's
          townnumber was found.
          // ...
          $sql = "select field2 from table1 where townnum={$_POST['townnum']}";
          $resource = mysql_query($sq l);
          if (!$resource) {
          // something wrong with the query.
          // for simplicity sake we just exit with a message
          exit('Error in query: ' . mysql_error());
          }
          I then want to assign a variable to the value of the field2 in that record
          If he types in 3 my code should assign the value of def to $var2
          if (mysql_num_rows ($resource) == 1) {
          $var2 = mysql_result($r esource, 0);
          } else {
          // Either $_POST['townnum'] does not exist, or it is not unique
          // anyway, for simplicity sake, we exit with a message
          exit('Invalid townnum. Go back and try again.');
          }
          // no more need for the resource
          mysql_free_resu lt($resource);
          // ...
          // $var2 is now the corresponding value from table1


          --
          File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

          Comment

          Working...