PHP PDO invokes a MySQL stored procedure instance with out parameters

Source: Internet
Author: User
Tags commit dsn php database prepare rollback stmt

The

Recently used a PHP database to access the latest technology POD, not packaged, and the code is written very little. I'll post my code below.

Code part:

<?php   $DSN  =  "Mysql:host=localhost;dbname=mysqldatabasedemo";   $db  = new pdo ($dsn, ' root ', ' 123456@# ', Array (pdo::mysql_attr_init_command => )
SET    names ' GBK '));
   $someInParameter 1= ' hellow ';
   $stmt  =  $db->prepare ("Call outputprocudure (: AddName, @outASD)");
   $stmt->bindparam (': AddName ',  $someInParameter 1);    if ($stmt->execute ())    {       $rs = $db->
Query (' select  @outASD ')->fetchall ();
       $dataArray = $rs [0];       echo ("<script type= ' Text/javascript ' > alert" + $dataArray [
0]+ "');</script>"); &NBSP;&NBSP;&NBSP}    else    {    echo ("<script type
= ' Text/javascript ' > alert (' Insert failed ');</script> ");    }?>



MySQL Stored procedures:




Php_pdo A stored procedure that calls MySQL with return parameters

DROP PROCEDURE IF EXISTS pro_test;
CREATE PROCEDURE Pro_test (in Val VARCHAR (m), out RTN int)
begin
Declare err int d  Efault 0;
-If an exception occurs, it is automatically processed and ROLLBACK
declare exit handler for SqlException ROLLBACK;
--Start the transaction start
transaction;
insert into test_user values (NULL,1, ' ah is a junior ');  - - set err = @ @IDENTITY; -- =    Gets the id;   set  of the last insert ERR&NBSP;=LAST_INSERT_ID (); --  gets the last inserted id   insert into test_user values ( Null,val,err);     --  Run no exception, COMMIT transaction    commit;  --  set return value is 1    set rtn=1;   end; 
$name = ' Chengdu city ';  
$stmt = $db->prepare ("Call Pro_test (?, @sp_result);");  
$stmt->bindparam (1, $name);  
$stmt->execute ();
$outputArray = $db->query ("select @sp_result")->fetch (PDO::FETCH_ASSOC);
<span style= "font-family:arial, Helvetica, Sans-serif;" > </span><span style= "font-family:arial, Helvetica, Sans-serif;" >print_r ($outputArray ["@sp_result"]);</span>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.