0

I m using php PDO as db connector. I am having problem in binding a email address say i have an sql

$sql = UPDATE user set email = :email where uid = 10

$smt = $dbh->prepare($sql);

$smt->bindParam(':email', '[email protected]');

The PDO is leaving every thing after @.

Thanx

1
  • 1
    What do you mean by "leaving every thing after @" Commented Jan 26, 2011 at 21:32

2 Answers 2

5

bindParam is used to bind a variable, not a value. I think what you want to do is use bindValue instead.

Sign up to request clarification or add additional context in comments.

2 Comments

you answer is incorrect. In this case the stored procedure is related to an I/O parameter sending data (UPDATE). For this very reason, you should use bindParam and pass the parameter by reference to have it properly evaluated.
@cpugourou no, my answer is 100% correct. OP is trying to bind a value (the string '[email protected]'), but is using bindParam, which is meant for binding variables by reference. Changing this to bindValue will fix this. Quick example at 3v4l.org/8kjBd to show the difference.
-1
define("SQLHOST", "127.0.0.1");
define("SQLUSER", "user");
define("SQLPASS", "password");
define("SQLSGBD", "database");

try {
    $dbh = new PDO('mysql:host=' . SQLHOST . ';dbname=' . SQLSGBD . ';charset=UTF8', SQLUSER, SQLPASS);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    var_dump('Connection failed: ' . $e->getMessage());
}

$emailList = ['[email protected]', '[email protected]'];
$stmt = $dbh->prepare("UPDATE user set email = ? where uid = 10");
foreach ($emailList as $key => $email) {
    $stmt->bindParam(1, $email, PDO::PARAM_STR);
    $stmt->execute();
    $obj = $stmt->fetchall(PDO::FETCH_ASSOC);
    if (count($obj) !== 0) {
        /* whatever */
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.