delete from database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sheldon Glickler

    delete from database

    I have code that does not delete from a database. The same code (cut an
    paste in the same file, but different function and having a different query)
    works. So, of course, I tested the query interactively. I echoed the query
    and did a cut and paste when interactively connect to the database. That
    one worked so it isn't the query since interactively it is seeing exactly
    the same thing as the code produces.

    Here is the code:

    function deleteCatalog($ catalog) {
    $query = "DELETE FROM CatalogNames WHERE sCatalogID='" . $catalog . "'";
    mssql_select_db ($database_Logi n, $_SESSION['Login']);
    $result = mssql_query($qu ery, $Login);
    ...
    }

    The $query echos:
    DELETE FROM CatalogNames WHERE sCatalogID='CMP '

    As an example of code that works (in the same file)
    function addToCatalog($c at_id, $cat_name) {
    $query = "INSERT INTO CatalogNames (sCatalogID, sCatalogName) " .
    "VALUES ('" . $cat_id . "', '" . $cat_name . "')";
    mssql_select_db ($database_Logi n, $_SESSION['Login']);
    $result = mssql_query($qu ery, $_SESSION['Login']);
    ...
    }


    The second and third lines are identical (cut and pasted) as in other places
    in the same code. In fact, it is only in this file that I do all the
    database work. Every other function works. This is the only delete,
    however.

    Any ideas?

    Shelly


  • J.O. Aho

    #2
    Re: delete from database

    Sheldon Glickler wrote:[color=blue]
    > I have code that does not delete from a database. The same code (cut an
    > paste in the same file, but different function and having a different query)
    > works. So, of course, I tested the query interactively. I echoed the query
    > and did a cut and paste when interactively connect to the database. That
    > one worked so it isn't the query since interactively it is seeing exactly
    > the same thing as the code produces.[/color]
    [color=blue]
    > $result = mssql_query($qu ery, $Login);
    > $result = mssql_query($qu ery, $_SESSION['Login']);[/color]

    Looking at the query function in your two functions gives this main
    difference. As deleteCatalog() hasn't the variable $Login assigned any value,
    it will be the same as Null which isn't a valid resource link definer, either
    you change the function to use the following line


    $result = mssql_query($qu ery);

    or

    $result = mssql_query($qu ery, $_SESSION['Login']);



    //Aho

    Comment

    • Sheldon Glickler

      #3
      Re: delete from database


      "J.O. Aho" <user@example.n et> wrote in message
      news:46kgi5Fbn0 u2U1@individual .net...[color=blue][color=green]
      >> $result = mssql_query($qu ery, $Login);
      >> $result = mssql_query($qu ery, $_SESSION['Login']);[/color]
      >
      > Looking at the query function in your two functions gives this main
      > difference. As deleteCatalog() hasn't the variable $Login assigned any
      > value, it will be the same as Null which isn't a valid resource link
      > definer, either you change the function to use the following line[/color]

      Cut and paste error when writing this orogonal post. Here is the latest and
      greatest for the two and the results are still that the delete doesn't work
      and the insert does.

      $query = "DELETE FROM CatalogNames WHERE sCatalogID='" . $catalog . "'";
      mssql_select_db ($database_Logi n, $_SESSION['Login']);
      $result = mssql_query($qu ery, $_SESSION['Login']);


      $query = "INSERT INTO CatalogNames (sCatalogID, sCatalogName) " .
      "VALUES ('" . $cat_id . "', '" . $cat_name . "')";
      mssql_select_db ($database_Logi n, $_SESSION['Login']);
      $result = mssql_query($qu ery, $_SESSION['Login']);


      Comment

      • J.O. Aho

        #4
        Re: delete from database

        Sheldon Glickler wrote:[color=blue]
        > "J.O. Aho" <user@example.n et> wrote in message
        > news:46kgi5Fbn0 u2U1@individual .net...[color=green][color=darkred]
        >>> $result = mssql_query($qu ery, $Login);
        >>> $result = mssql_query($qu ery, $_SESSION['Login']);[/color]
        >> Looking at the query function in your two functions gives this main
        >> difference. As deleteCatalog() hasn't the variable $Login assigned any
        >> value, it will be the same as Null which isn't a valid resource link
        >> definer, either you change the function to use the following line[/color]
        >
        > Cut and paste error when writing this orogonal post. Here is the latest and
        > greatest for the two and the results are still that the delete doesn't work
        > and the insert does.[/color]

        Okey, then lets look at the $_SESSION['Login'], how do you set it and do you
        really store that in a session and reuse it? How do you make the mssql_connect() ?


        //Aho

        Comment

        • Steve

          #5
          Re: delete from database

          On Tue, 28 Feb 2006 21:06:58 -0500, Sheldon Glickler wrote:
          [color=blue]
          > I have code that does not delete from a database. The same code (cut an
          > paste in the same file, but different function and having a different query)
          > works. So, of course, I tested the query interactively. I echoed the query
          > and did a cut and paste when interactively connect to the database. That
          > one worked so it isn't the query since interactively it is seeing exactly
          > the same thing as the code produces.
          >
          > Here is the code:
          >
          > function deleteCatalog($ catalog) {
          > $query = "DELETE FROM CatalogNames WHERE sCatalogID='" . $catalog . "'";
          > mssql_select_db ($database_Logi n, $_SESSION['Login']);
          > $result = mssql_query($qu ery, $Login);
          > ...
          > }
          >
          > The $query echos:
          > DELETE FROM CatalogNames WHERE sCatalogID='CMP '
          >
          > As an example of code that works (in the same file)
          > function addToCatalog($c at_id, $cat_name) {
          > $query = "INSERT INTO CatalogNames (sCatalogID, sCatalogName) " .
          > "VALUES ('" . $cat_id . "', '" . $cat_name . "')";
          > mssql_select_db ($database_Logi n, $_SESSION['Login']);
          > $result = mssql_query($qu ery, $_SESSION['Login']);
          > ...
          > }
          >
          >
          > The second and third lines are identical (cut and pasted) as in other places
          > in the same code. In fact, it is only in this file that I do all the
          > database work. Every other function works. This is the only delete,
          > however.
          >
          > Any ideas?
          >
          > Shelly[/color]

          Some error handling *might* shed some light onto the problem???

          Comment

          • Steve

            #6
            Re: delete from database

            [color=blue]
            > $query = "DELETE FROM CatalogNames WHERE sCatalogID='" . $catalog . "'";
            > mssql_select_db ($database_Logi n, $_SESSION['Login']);
            > $result = mssql_query($qu ery, $_SESSION['Login']);
            >
            >
            > $query = "INSERT INTO CatalogNames (sCatalogID, sCatalogName) " .
            > "VALUES ('" . $cat_id . "', '" . $cat_name . "')";
            > mssql_select_db ($database_Logi n, $_SESSION['Login']);
            > $result = mssql_query($qu ery, $_SESSION['Login']);[/color]

            I think it unlikely that you can reliably cache and reuse a database
            resource between sessions. You should create a new session with each
            invocation of the script.

            ---
            Steve

            Comment

            • Sheldon Glickler

              #7
              Re: delete from database


              "J.O. Aho" <user@example.n et> wrote in message
              news:46kjr6Fbl4 rbU1@individual .net...[color=blue]
              > Sheldon Glickler wrote:[color=green]
              >> "J.O. Aho" <user@example.n et> wrote in message
              >> news:46kgi5Fbn0 u2U1@individual .net...[color=darkred]
              >>>> $result = mssql_query($qu ery, $Login);
              >>>> $result = mssql_query($qu ery, $_SESSION['Login']);
              >>> Looking at the query function in your two functions gives this main
              >>> difference. As deleteCatalog() hasn't the variable $Login assigned any
              >>> value, it will be the same as Null which isn't a valid resource link
              >>> definer, either you change the function to use the following line[/color]
              >>
              >> Cut and paste error when writing this orogonal post. Here is the latest
              >> and greatest for the two and the results are still that the delete
              >> doesn't work and the insert does.[/color]
              >
              > Okey, then lets look at the $_SESSION['Login'], how do you set it and do
              > you really store that in a session and reuse it? How do you make the
              > mssql_connect() ?[/color]

              Yes, I do store it and it is there. It worked for all the others.

              $hostname_Login = "localhost" ;
              $database_Login = "the database name";
              $username_Login = "the username";
              $password_Login = "the password";
              $apbLogin = mssql_pconnect( $hostname_Login , $username_Login ,
              $password_Login ) or die(mysql_error ());




              Comment

              • Sheldon Glickler

                #8
                Re: delete from database


                "Steve" <ThisOne@Aint.V alid> wrote in message
                news:pan.2006.0 3.01.09.52.57.4 77061@Aint.Vali d...[color=blue]
                > On Tue, 28 Feb 2006 21:06:58 -0500, Sheldon Glickler wrote:
                >[color=green]
                >> I have code that does not delete from a database. The same code (cut an
                >> paste in the same file, but different function and having a different
                >> query)
                >> works. So, of course, I tested the query interactively. I echoed the
                >> query
                >> and did a cut and paste when interactively connect to the database. That
                >> one worked so it isn't the query since interactively it is seeing exactly
                >> the same thing as the code produces.
                >>
                >> Here is the code:
                >>
                >> function deleteCatalog($ catalog) {
                >> $query = "DELETE FROM CatalogNames WHERE sCatalogID='" . $catalog .
                >> "'";
                >> mssql_select_db ($database_Logi n, $_SESSION['Login']);
                >> $result = mssql_query($qu ery, $Login);
                >> ...
                >> }
                >>
                >> The $query echos:
                >> DELETE FROM CatalogNames WHERE sCatalogID='CMP '
                >>
                >> As an example of code that works (in the same file)
                >> function addToCatalog($c at_id, $cat_name) {
                >> $query = "INSERT INTO CatalogNames (sCatalogID, sCatalogName) " .
                >> "VALUES ('" . $cat_id . "', '" . $cat_name . "')";
                >> mssql_select_db ($database_Logi n, $_SESSION['Login']);
                >> $result = mssql_query($qu ery, $_SESSION['Login']);
                >> ...
                >> }
                >>
                >>
                >> The second and third lines are identical (cut and pasted) as in other
                >> places
                >> in the same code. In fact, it is only in this file that I do all the
                >> database work. Every other function works. This is the only delete,
                >> however.
                >>
                >> Any ideas?
                >>
                >> Shelly[/color]
                >
                > Some error handling *might* shed some light onto the problem???[/color]

                I have an

                if (!$result)
                print the error including the query

                That is how I got the query that I copied and used interactively
                successfully. Everything before that looked fine.


                Comment

                • Sheldon Glickler

                  #9
                  Re: delete from database


                  "Steve" <google.spam@na stysoft.com> wrote in message
                  news:1141211513 .214654.269950@ z34g2000cwc.goo glegroups.com.. .[color=blue]
                  >[color=green]
                  >> $query = "DELETE FROM CatalogNames WHERE sCatalogID='" . $catalog .
                  >> "'";
                  >> mssql_select_db ($database_Logi n, $_SESSION['Login']);
                  >> $result = mssql_query($qu ery, $_SESSION['Login']);
                  >>
                  >>
                  >> $query = "INSERT INTO CatalogNames (sCatalogID, sCatalogName) " .
                  >> "VALUES ('" . $cat_id . "', '" . $cat_name . "')";
                  >> mssql_select_db ($database_Logi n, $_SESSION['Login']);
                  >> $result = mssql_query($qu ery, $_SESSION['Login']);[/color]
                  >
                  > I think it unlikely that you can reliably cache and reuse a database
                  > resource between sessions. You should create a new session with each
                  > invocation of the script.[/color]

                  This is one session where it works on one and fails on the other. I use a
                  mssql_pconnect, but I will try making a new connection.


                  Comment

                  • Sheldon Glickler

                    #10
                    Re: delete from database


                    "Sheldon Glickler" <sheldonlg@bell south.net> wrote in message
                    news:tOfNf.4224 1$X7.34902@bign ews7.bellsouth. net...[color=blue]
                    > $hostname_Login = "localhost" ;
                    > $database_Login = "the database name";
                    > $username_Login = "the username";
                    > $password_Login = "the password";
                    > $apbLogin = mssql_pconnect( $hostname_Login , $username_Login ,
                    > $password_Login ) or die(mysql_error ());[/color]

                    cut and paste error. make that[color=blue]
                    > $Login = mssql_pconnect( $hostname_Login , $username_Login ,[/color]

                    I thought I changed removed all the apb's for the posting. Missed one The
                    original has it all there. I just wanted to make it as neutral as possible
                    for posting.


                    Comment

                    • J.O. Aho

                      #11
                      Re: delete from database

                      Sheldon Glickler wrote:[color=blue]
                      > "J.O. Aho" <user@example.n et> wrote in message[/color]

                      It's really better if you do cut'n'paste the code as it is (of course for
                      security reasons not the login/password for your database) than modify it for
                      nice looking when you post, as you may manage to fix you problem when you edit
                      the code for posting and that way not will be able to detect the real problem.
                      [color=blue][color=green]
                      >> Okey, then lets look at the $_SESSION['Login'], how do you set it and do
                      >> you really store that in a session and reuse it? How do you make the
                      >> mssql_connect() ?[/color]
                      >
                      > Yes, I do store it and it is there. It worked for all the others.
                      >
                      > $hostname_Login = "localhost" ;
                      > $database_Login = "the database name";
                      > $username_Login = "the username";
                      > $password_Login = "the password";
                      > $Login = mssql_pconnect( $hostname_Login , $username_Login ,
                      > $password_Login ) or die(mysql_error ());[/color]

                      According to the code here you store the resource in $Login but in functions
                      you use $_SESSION['Login'], which is a reuse of a older resource which don't
                      have to be the same as current resource.

                      You should use the current one, there is always the risk that database has
                      closed the connection that the resource is pointing at, which makes things to
                      fail. You should use something like

                      $Login = mssql_pconnect( $hostname_Login , $username_Login , $password_Login );
                      if(!$Login) {
                      echo "Theres is problem to connect with the database<br>\n" ;
                      exit;
                      }

                      die(mysql_error ()) won't work for you, as you aren't using mysql, the closest
                      you can use would be die(mssql_get_l ast_message()) but the information from
                      mssql suxx quite a lot.

                      Use the $Login instead of the $_SESSION['Login'], this will most likely
                      require you to add the following line to your own functions

                      global $Login;

                      or add another parameter for your functions

                      function deleteCatalog($ catalog,$sql_re source) {
                      $query = "DELETE FROM CatalogNames WHERE sCatalogID='" . $catalog . "'";
                      mssql_select_db ($database_Logi n, $sql_resource);
                      $result = mssql_query($qu ery, $sql_resource);
                      ...
                      }


                      //Aho

                      Comment

                      • sheldonlg@gmail.com

                        #12
                        Re: delete from database

                        Problem solved. I want to thank all for their help. The problem was
                        with the mssql_select_db in that the function did not know what the
                        $database_Login was. I changed my code to select the database first,
                        and then pass in the resource to the function.

                        Thanks again.

                        Comment

                        Working...