-2

I am not a professional, just started learning PHP coding.

I am having some issues getting PDO to work inside a function. Hope someone will show me the correct way to code it inside a function.

function testKey($key){
// CHECK IF LINK IS AVAILABLE IN THE DATABASE
$result = dbConnect()->prepare("SELECT _uniq_key FROM _link_key WHERE 
_uniq_key= :1 AND _active= :2");
    $result->bindParam(':1', $key);
    $result->bindParam(':2', $o);
    $result->execute();
    $rows = $result->fetch(PDO::FETCH_NUM);
        if($rows > 0) {

            echo 'link is available';
            checkLink();

        }
        else 
        {

            echo 'link does not exist!';
        }
}

The PDO doesn't work correctly, and won't execute the PDO script.

Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
Root X
  • 21
  • 1
  • 3

3 Answers3

1
function testKey($key){
    // global $key; BAD
    // Assuming that active must be 1
    $active = 1;
    // CHECK IF LINK IS AVAILABLE IN THE DATABASE
    // Assuming that dbConnect() makes the correct PDO object
    $result = dbConnect()->prepare("SELECT _uniq_key FROM _link_key WHERE _uniq_key= :1 AND _active= :2");
    $result->bindParam(':1', $key);
    $result->bindParam(':2', $active); // only find active records
    $result->execute();
    $rows = $result->fetch(PDO::FETCH_NUM);

    if( $rows !== false ) {
        // http://php.net/manual/en/pdostatement.fetch.php
        // If no record, this function will also return false.
        echo 'link is available';
        checkLink(); // not sure what this does
    }
    else 
    {
        echo 'link does not exist!';
    }
}

Now call the function:

$mySpecialKey = 'mango';
testKey($mySpecialKey); // note parameter is passed here
ryantxr
  • 4,119
  • 1
  • 11
  • 25
-2

Found my error, working code:

function testKey($key){
global $key;
// CHECK IF LINK IS AVAILABLE IN THE DATABASE
$result = dbConnect()->prepare("SELECT _uniq_key FROM _link_key WHERE 
_uniq_key= :1 AND _active= :2");
$result->bindParam(':1', $key);
$result->bindParam(':2', $o);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
    if($rows > 0) {

        echo 'link is available';
        checkLink();

    }
    else 
    {

        echo 'link does not exist!';
    }
}
Root X
  • 21
  • 1
  • 3
  • 1
    Where does `$o` come from? For this to be a valid answer please explain what you learned so it will benefit others. Honestly makes no sense you don't pass an argument and use *global* keyword. Alternately close your question. – ficuscr Aug 22 '17 at 21:04
  • 1
    This is a bad answer. Adding `global` to make it work means that passing it as a parameter is being ignored. – ryantxr Aug 22 '17 at 21:15
  • post your entire code. I think there are many things you don't understand and implement incorrectly. Someone will show you the correct way. – Silencer310 Aug 22 '17 at 21:22
-2

little adjustment here

 if($rows > 0) {

    echo 'link is available';
    checkLink();

}
else 
{

    echo 'link does not exist!';
}

Try this

 if($result->rowCount() > 0) {

    echo 'link is available';
    checkLink();

}
else 
{

    echo 'link does not exist!';
}