how to retrieve data containg ampersand from mysql table using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • benoypaul
    New Member
    • May 2007
    • 28

    how to retrieve data containg ampersand from mysql table using php

    Code:
    $CAT='Bath & Body';
    sql="select * from Products where Category = '$CAT';
    $result=mysql_query($sql);
    When I am running above code , I got an error. When I am removing '&' from $CAT , it is working fine. I wish to know how to include ampersand in mysql data.

    Thanks in advance
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by benoypaul
    Code:
    $CAT='Bath & Body';
    sql="select * from Products where Category = '$CAT';
    $result=mysql_query($sql);
    When I am running above code , I got an error. When I am removing '&' from $CAT , it is working fine. I wish to know how to include ampersand in mysql data.

    Thanks in advance
    Try preceeding it with a backslash
    [php]
    $CAT = "something \& something else";
    [/php]

    Comment

    • benoypaul
      New Member
      • May 2007
      • 28

      #3
      Sorry. It is not working

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by benoypaul
        Sorry. It is not working
        Strange
        What error do you get?

        Comment

        • benoypaul
          New Member
          • May 2007
          • 28

          #5
          I am using this php code in a javascript function. I got the error "object expected". When removing ampersand from $CAT or commenting $sql stament, then code is working properly.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by benoypaul
            I am using this php code in a javascript function. I got the error "object expected". When removing ampersand from $CAT or commenting $sql stament, then code is working properly.
            Can you please show the full code
            using the proper code tags

            Comment

            • benoypaul
              New Member
              • May 2007
              • 28

              #7
              Code:
              function removeAllOptions(selectbox)
              {
              	var i;
              	for(i=selectbox.options.length-1;i>=0;i--)
              	{
              		//selectbox.options.remove(i);
              		selectbox.remove(i);
              	}
              }
              
              
              function addOption(selectbox, value, text )
              {
              	var optn = document.createElement("OPTION");
              	optn.text = text;
              	optn.value = value;
                 
              	selectbox.options.add(optn);
              }
              
              
              function SelectEditProduct()
              {
              removeAllOptions(document.editproducts.productname);
              addOption(document.editproducts.productname,"","select");
              <?php
              include_once("database.php");
              $sql="select * from ProductCategory ";
              $result1=mysql_query($sql) ;
              while ($rows1=mysql_fetch_row($result1))
              {
              echo "if(document.editproducts.categoryname.value == '$rows1[1]'){";
              
              $sql="select * from Products where Category ='$rows1[1]'";  // THIS LINE SHOWS ERROR
              //$sql="select * from Products where Category='Bath \& Body'";
               
              $result=mysql_query($sql);
              while ($rows=mysql_fetch_row($result))
              {
               echo "addOption(document.editproducts.productname,'$rows[1]', '$rows[2]');";
              }
              echo "}";  
              }
              
              ?>
              
              }
              ProductCategory table contains some data with ampersand ($rows1[1]). This causes errors

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Can you show me the output you get on the page source?
                I'm still not understand your problem.
                Is the error with javascript or php?

                Comment

                • benoypaul
                  New Member
                  • May 2007
                  • 28

                  #9
                  It is a javascript error. When we removing $sql ="..." statement, there is no errors. URL of this code is http://www.markdickson.com/adminpanel/editproduct.php

                  Error occur when we changing productcategory

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Originally posted by benoypaul
                    It is a javascript error. When we removing $sql ="..." statement, there is no errors. URL of this code is http://www.markdickson.com/adminpanel/editproduct.php

                    Error occur when we changing productcategory
                    If it's a javascript error, you should ask in the javascript forum.

                    Comment

                    • benoypaul
                      New Member
                      • May 2007
                      • 28

                      #11
                      Thanks a lot for your help

                      Comment

                      Working...