dongzhuo7291 2012-09-30 04:28 采纳率: 100%
浏览 180
已采纳

在php中使用'out'参数调用mysql函数或存储过程

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.

  • 写回答

2条回答 默认 最新

  • doushi8599 2012-10-03 04:19
    关注

    i found solution for this

    http://blog.ulf-wendel.de/2011/using-mysql-stored-procedures-with-php-mysqli/

      $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 2012-09-30 04:32
    关注

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

    <?php
        $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
        $stmt->execute();
    
        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.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信实时共享位置修改
  • ¥100 TG的session协议号转成直登号号后客户端登录几分钟后自动退出设备
  • ¥30 共模反馈回路的小信号增益
  • ¥15 arduino ssd1306函数与tone函数放歌代码不兼容问题
  • ¥70 0.96版本hbase的row_key里含有双引号,无法deleteall
  • ¥15 Jupyter怎么一行一行运行
  • ¥20 Ida Pro增加插件出现问题
  • ¥15 诊断性META分析合并效能的检验
  • ¥15 请问abb根据色块判断奇偶数并根据批次号放入仓储
  • ¥15 .Net Core Winfrom 承载SignalR