SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Checking boolean of single mysql cell and returning value to if-else

    Hey everyone,

    I have a page where a person enters an order number to check if it is processed. The php checks the table for the ordernumber and then checks the corresponding "processed" column where I have a boolean 1 or 0.

    Im having trouble returning the boolean and passing it through an if-else statement.
    I then want to output "Processed" if 1 and "Not Processed" if 0.

    At this point, any order number I enter returns a "Processed" value.
    I know the issue is getting the query result and passing it through the if-else.

    Any help would be greatly appreciated, thanks!

    PHP Code:
    <?php 
    if(isset($_POST['checktrans'])) {

    $db mysql_connect("mysql.xxxxxxx.com","xxxxxxx","xxxxxxxxx");
       if(!
    $db) die("Error connecting to MySQL database.");
       
    mysql_select_db("xxxxxxxx" ,$db);

        
    $check $_POST['checktrans'];
      
    //don't need LIMIT 1 as the ID should be unique (auto-incremented)
      
    $sql "SELECT `process` FROM `bits` WHERE `ordernumber` = $check";
      
    mysql_query($sql) or die(mysql_error());
      
      
    $result mysql_query($sql);
      
      
      if (
    mysql_free_result($result) == 1) {
          echo 
    "PROCESSED";
      } elseif (
    mysql_free_result($result) == 0) {
          echo 
    "NOT PROCESSED";
      }
    }


    ?>

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    For some reason you are calling mysql_query two times and mysql_free_result frees memory instead of fetching result. Also you shouldn't use values directly from request in your query, at least mysql_real_escape_string can be used. Even better would be also validating that received data is what you are expecting.
    Here's snippet for checking if order has been processed.
    PHP Code:
    ...
    $sql "SELECT `process` FROM `bits` WHERE `ordernumber` = $check";    
    $result mysql_query($sql) or die(mysql_error()); 
    $row mysql_fetch_assoc($result)
       
    if (
    $row['process']  == 1) { 
          echo 
    "PROCESSED"
    } else { 
          echo 
    "NOT PROCESSED"
    }

    mysql_free_result($result);

    ... 
    Also it's recommended to use MySQLi or PDO_MuSQL extensions for MySQL access instead of old MySQL extension. More info in php manual.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •