2012-09-30 04:28



I have used mysql stored procedure in php, but i need to return few values from my sql, so i am planning to use mysql functions or stored procedure with ''out'parameter.

it would be good if anyone can help me on that.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doushi8599 doushi8599 9年前

    i found solution for this

      $mysqli = new mysqli("localhost", "root", "", "test");
    if (!$mysqli->query("DROP PROCEDURE IF EXISTS p") ||
        !$mysqli->query('CREATE PROCEDURE p(OUT msg VARCHAR(50)) BEGIN SELECT "Hi!" INTO msg; END;'))
       echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
    if (!$mysqli->query("SET @msg = ''") ||
        !$mysqli->query("CALL p(@msg)"))
     echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
    if (!($res = $mysqli->query("SELECT @msg as _p_out")))
     echo "Fetch failed: (" . $mysqli->errno . ") " . $mysqli->error;
    $row = $res->fetch_assoc();
    echo $row['_p_out'];
    点赞 评论 复制链接分享
  • duanjing7651 duanjing7651 9年前

    If you are using PHP PDO extension, try this code below

        $stmt = $dbh->prepare("CALL sp_returns_string(?)");
        // assumes that the output is VARCHAR(100) 
        $stmt->bindParam(1, $return_value, PDO::PARAM_STR, 100); 
        // call the stored procedure
        print "procedure returned $return_value

    Output parameters are slightly more complex to use than input parameters, in that a developer must know how large a given parameter might be when they bind it. If the value turns out to be larger than the size they suggested, an error is raised.

    点赞 评论 复制链接分享