简体   繁体   English

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

[英]pdo not reading string from mysql function

I am using the following code for reading a String from a mysql function: 我使用以下代码从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();
}
?>

This is not reading the returned value , however if I read the value usimg mysql* like : 这不是读取返回值,但是如果我读取值usimg mysql *就像:

<?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;
        }

?>

This returns correctly . 这会正确返回。 ANy idea wha am I missing ? 我有什么想法,我错过了吗?

function : 功能:

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

code that worked for PDO :: 适用于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();
    }

?>

原因是,您没有从查询中获取结果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM