pdo没有从mysql函数中读取字符串

时间:2012-08-26 16:19:48

标签: php mysql pdo

我使用以下代码从mysql函数中读取String

<?php
print_r($_POST);
try {
    $dbh = new PDO("mysql:dbname=mydb;host=myhost", "myuser", "mypass" );

    $value = $_POST['myLname'];

    print $value ;
    //print $dbh ;

    $stmt = $dbh->prepare("CALL check_user_exists(?)");
    $stmt->bindParam(1, $value, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 50);

    // call the stored procedure
    $stmt->execute();

    print "procedure returned $value\n";

    echo "PDO connection object created";
    $dbh = null;

} catch(PDOException $e) {
    echo $e->getMessage();
}
?>

这不是读取返回值,但是如果我读取值usimg mysql * like:

<?php

$dbhost='myhost';
        $dbuser='mydb';
        $dbpassword='mypass';
        $db='mydb';

        $con=mysql_connect($dbhost, $dbuser, $dbpassword) or die("Could not connect: " . mysql_error()); ;
        mysql_select_db($db,$con);
        $qry_str = "select check_user_exists('chadhass@hotmail.com')";

        $rset = mysql_query($qry_str) or exit(mysql_error());
                $row = mysql_fetch_assoc($rset);
                mysql_close($con);
        foreach($row as $k=>$v)
        {
            print $k.'=>'.$v;
        }

?>

这会正确返回。我有什么想法,我错过了吗?

功能:

CREATE
FUNCTION `check_user_exists`(in_email
VARCHAR(100)) RETURNS varchar(1) CHARSET utf8
READS SQL DATA
BEGIN
DECLARE vcount INT;
DECLARE vcount1 INT;
SELECT COUNT(*) INTO vcount FROM USERS
WHERE USEREMAIL=in_email;
IF vcount=1 then
SELECT COUNT(*) INTO vcount1 FROM USERS
WHERE USEREMAIL=in_email and isactive=1;
if vcount1=1 then
return('1');
else
return('0');
end if;
ELSE
RETURN('2');
END IF;
END

适用于PDO ::

的代码
<?php
//print_r($_POST);
try {
    $dbh = new PDO(PDO("mysql:dbname=mydb;host=myhost", "myuser", "mypass" ); 



    $value = $_POST['myLname'];


    $result = $dbh->prepare("select check_user_exists(?) as retval");
    $result->bindParam(1, $value, PDO::PARAM_STR, 2);
    $result->setFetchMode(PDO::FETCH_CLASS, 'stdClass');
    $result->execute();
    $obj = $result->fetch();

print($obj->retval);




    echo "PDO connection object created";
    $dbh = null;
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

?>

1 个答案:

答案 0 :(得分:4)

原因是,您无法从查询中获取结果。

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);