PHP - MySQL gets value of out parameter from a stored procedure

前端 未结 5 2028
自闭症患者
自闭症患者 2021-01-06 13:20

I have called a MySQL stored procedure from PHP using mysqli. This has one out parameter.

$rs = $mysqli->query(\"CALL addNewUser($name,$age,@         


        
5条回答
  •  隐瞒了意图╮
    2021-01-06 14:06

    Or even just do a "SELECT @id AS id" then $row->id will work fine. I always rename select columns to keep the name meaningful when necessary :-)

    BTW, you can simply concatenate the call and select @... (with a ; statement delimiter) and the RS will be the returned value. Unfortunately this returns a mutli-resultset and you need to flush the full set otherwise the subsequent queries will stall. See following examples:

    $db->multi_query( "CALL addNewUser($name,$age,@id);SELECT @id as id" );
    $db->next_result();            // flush the null RS from the call
    $rs=$db->store_result();       // get the RS containing the id
    echo $rs->fetch_object()->id, "\n";
    $rs->free();
    

    Alternatively add the select into the addNewUser and return a RS instead of out param

    $rs = $db->query( "CALL addNewUser($name,$age)" );
    echo $rs->fetch_object()->id, "\n";
    $rs->close();
    $db->next_result();            // flush the null RS from the call
    

    The first returns a multiquery (NULL, RS) set and the second a (RS, NULL) set, hence you can use a simple query() call which embeds the first fetch_object(), but you still need to flush the RS stack.

提交回复
热议问题