php mysql query returning empty set

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • subu
    New Member
    • Mar 2007
    • 4

    php mysql query returning empty set

    the following query is returning an empty set and i am not sure why:

    if (($OutputPower == '75') || ($OutputPower == 'N/A'))
    {
    sql = Select * from $Tname1 WHERE 1".(($OutputPow er != 'N/A') ? "AND `Output Power` = '$OutputPower'" : "" );

    so essentially it includes Output Power in the query as long as the user did not enter N/A.

    it works fine if i just have:
    sql = Select * from $Tname1 WHERE 1 AND `Output Power` = '$OutputPower'" ;

    but this only considers the case when user enters 75 W.

    any thoughts are appreciated
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    If $OutputPower is N/A you will get an empty set.

    Originally posted by subu
    the following query is returning an empty set and i am not sure why:

    if (($OutputPower == '75') || ($OutputPower == 'N/A'))
    {
    sql = Select * from $Tname1 WHERE 1".(($OutputPow er != 'N/A') ? "AND `Output Power` = '$OutputPower'" : "" );

    so essentially it includes Output Power in the query as long as the user did not enter N/A.

    it works fine if i just have:
    sql = Select * from $Tname1 WHERE 1 AND `Output Power` = '$OutputPower'" ;

    but this only considers the case when user enters 75 W.

    any thoughts are appreciated

    Comment

    • subu
      New Member
      • Mar 2007
      • 4

      #3
      Originally posted by Motoma
      If $OutputPower is N/A you will get an empty set.
      not quite...followi ng is the query when OutputPower == N/A:

      sql = "SELECT * FROM $tname1 WHERE 1"

      when i run this query directly on the database through phpMyAdmin, it returns all the rows, which is what should be happening....

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        [PHP]Select * from $Tname1 WHERE `Output Power` = '$OutputPower'[/PHP]

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          What exactly are you trying to accomplish with this query?

          Comment

          • subu
            New Member
            • Mar 2007
            • 4

            #6
            its a web form with a sequence of questions (output power being one of them)....if the user chooses a certain value (eg:0-25 W), then i include it in my query and filter the results based on 0-25 W...if user chooses N/A, then i return all results...the following code works:
            if ((($OutputPower == '0-25 W') ||($OutputPower == '25-50 W'))
            {
            $sql = "SELECT * FROM $tname1 WHERE `Output Power` = '$OutputPower'" .(($ICSupply != 'N/A') ? "AND `IC Supply` = '$ICSupply'" : "" ).(($HVStartup != 'N/A') ? "AND `HV Startup` = '$HVStartup'" : "" );

            where IC Supply and HV Startup are other features which are either included or excluded from the query depending on user input....but i am considering the case when the user enters N/A for all questions, in which case the query would end up being:
            $sql = "SELECT * FROM $tname1 WHERE 1"

            this should work but is not...

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              Your best bet would be to echo your SQL statement before you perform the query.

              Comment

              • subu
                New Member
                • Mar 2007
                • 4

                #8
                thanks for the help

                Comment

                • Motoma
                  Recognized Expert Specialist
                  • Jan 2007
                  • 3236

                  #9
                  Originally posted by subu
                  thanks for the help
                  Anytime. Sorry I couldn't be of more use.

                  Comment

                  Working...