简体   繁体   English

如何从mysql函数访问php中的返回值

[英]how to access a return value in php from mysql function

I am using a msql function as follows.我正在使用 msql 函数,如下所示。

CREATE  FUNCTION `check_login`(p_username VARCHAR(30),p_password VARCHAR(30),role VARCHAR(20)) 
RETURNS BOOL
deterministic
    BEGIN
    DECLARE retval INT;
    Declare cid int;
            IF role = "customer" THEN

                select convert(p_username,unsigned integer)into cid;
                SELECT COUNT(custid) INTO retval FROM customer WHERE custid = cid and pwd = p_password;
                IF retval != 0 THEN
                    RETURN TRUE;    
                ELSE
                    RETURN FALSE;                           
                END IF;
            END IF;
    END$$
DELIMITER ;

And using the following php code.并使用以下 php 代码。

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$role=$_POST['role'];
$sql="select check_login('$myusername','$mypassword','$role')";
$result=mysql_query($con, $sql);
$row=mysql_result($row,1);
echo $row[1];

But am unable to get the return value.但是我无法获得返回值。

As mentioned in another comment, you should use a different MySQL adapter (PDO or mysqli).正如另一条评论中提到的,您应该使用不同的 MySQL 适配器(PDO 或 mysqli)。 However, if you must keep the adapter as is, you should change "mysql_result($row, 1)" to "mysql_result($row, 0)," as the results of a query are indexed starting from zero.但是,如果您必须保持适配器原样,您应该将“mysql_result($row, 1)”更改为“mysql_result($row, 0)”,因为查询的结果是从零开始索引的。 Alternatively, you could assign a name to the derived column and use "mysql_fetch_assoc" to return an associative array.或者,您可以为派生列分配一个名称并使用“mysql_fetch_assoc”返回关联数组。

$sql = 'SELECT check_login(\'$myusername\', \'$mypassword\', \'$role\')'; $vars = array( '$myusername' => $_POST['myusername'], '$mypassword' => $_POST['mypassword'], '$role' => $_POST['role'] ); $queryResult = $conn->query(strtr($sql, $vars)); $return = $queryResult->fetch_assoc(); foreach ($return as $return=> $functionReturn) { $return = $functionReturn; } echo $return;

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

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