0

Hei,

I need a function in php that checks if a value entered by form is already in database (sql - server -- PDO), and return TRUE or FALSE.

I tried to do this, but I got stuck and didn't found a solution on internet. could you give me a hint on how to threat the above condition ?

function check_code($code) {
GLOBAL $handler;
$code = check_input($code);
        try{
      $query2 = $handler -> prepare("SELECT code from stock where code = :code");
      $query2 -> execute(array(
                    ':code' => $code
                    )); 
return  >???<
        }
        catch(PDOException $e){
            echo $e -> getMessage();
      }  }
Ionut P.
  • 63
  • 10

2 Answers2

0

return something like row_count(result) > 0

murison
  • 3,640
  • 2
  • 23
  • 36
0

I've never work with sql-server before but I had worked with PDO many times, basically this is how would check in pdo

<?php
function check_code($code)
{
    GLOBAL $handler;

    $code = check_input($code);
    try {
        $query2 = $handler->prepare("SELECT code from stock where code = :code");
        $query2->execute(array(':code' => $code));
        $results = $query2->fetchColumn();
        if (count($results) > 0) {
            echo "exists";
        } else {

            echo "does not exist";
        }
    }
    catch (PDOException $e) {
        echo $e->getMessage();
    }
}
?>

NB: Avoid using the Global var... Stop using `global` in PHP

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34