Home > Backend Development > PHP Tutorial > php存储过程问题

php存储过程问题

WBOY
Release: 2016-06-20 12:38:39
Original
1041 people have browsed it


用php调用emp_id这个变量,怎么写?


回复讨论(解决方案)

<?php$emp_id = 199;$result = $dbh->query("call testit($emp_id)");print_r($result);?>
Copy after login

你这个还是获取不到emp_id的值

当然取不到!因为 emp_id 是传出的变量
要用 用户变量承接后在读取

mysql_query("call testit(@a)");$rs = mysql_query("select @a");print_r(mysql_fetch_assoc($rs));
Copy after login


数据为空,为毛没数据

mysql_query("call testit(@a)");
$rs = mysql_query("select @a");
print_r(mysql_fetch_assoc($rs));
用的这个代码

首先你要确认你的call testit() 在命令行能跑。。。

可以跑,不报错

call testit(123)

不要想了,不可能的
php_mysql 扩展创立的时候 mysql 还是在 版本 4 的时代。而 MySQL 4 尚不支持存储过程
自然 php_mysql 扩展在设计上也没有考虑到如何返回存储过程的多个结果
这要就是 php_mysql 扩展要被淘汰掉的主要原因之一

进入 MySQL 5 时代后,仅在 php_mysql 扩展上打补丁难度太大,所以就有了 php_mysqli 扩展

下面通过一些测试来进行观察

$create =<<< SQLCREATE PROCEDURE testit(out emp_id int)begin set emp_id=199; select emp_id as emps;end;SQL;$drop = 'DROP PROCEDURE IF EXISTS testit';
Copy after login
mysql_connect('localhost', 'root', '');mysql_select_db('test');mysql_query($drop);mysql_query($create);$rs = mysql_query('call testit(@b)') or die(mysql_error());print_r(mysql_fetch_assoc($rs));$rs = mysql_query('select @b') or die(mysql_error());print_r(mysql_fetch_assoc($rs));
Copy after login
Array(    [emps] => 199)Commands out of sync; you can't run this command now
Copy after login
$dsn = 'mysql:host=localhost;dbname=test';$options = array(  PDO::MYSQL_ATTR_INIT_COMMAND => "set names gbk",  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);try {  $dbh = new PDO($dsn, 'root', '', $options);  $dbh->query($drop);  $dbh->query($create);  $stmt = $dbh->prepare('call testit(@a)');//, array(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY));  $pp = 0;  $stmt->bindParam(1, $pp, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT, 12);  $stmt->execute();  do {    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);    if ($rows) {      print_r($rows);    }  } while ($stmt->nextRowset());  print_r($dbh->query('select @a')->fetchALL());} catch (PDOException $e) {  print "Error!: " . $e->getMessage() . PHP_EOL;}print_r($dbh->query('select @a')->fetchALL());
Copy after login
Array(    [0] => Array        (            [emps] => 199        ))Error!: SQLSTATE[HY000]: General errorArray(    [0] => Array        (            [@a] => 199        ))
Copy after login
$db = new mysqli('localhost', 'root', '', 'test');$db->query($drop);$db->query($create);$rs = $db->query('call testit(@a)');print_r($rs->fetch_assoc());$db->next_result();$rs = $db->query('select @a') or die($db->error);print_r($rs->fetch_assoc());
Copy after login
Array(    [emps] => 199)Array(    [@a] => 199)
Copy after login

关于存储过程的建立和 out 参数的使用,MySQL 手册中是这样举例的
一个使用OUT参数的简单的存储程序的例子。例子为,在程序被定义的时候,用mysql客户端delimiter命令来把语句定界符从 ;变为//。这就允许用在程序体中的;定界符被传递到服务器而不是被mysql自己来解释。mysql> delimiter // mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)    -> BEGIN    ->   SELECT COUNT(*) INTO param1 FROM t;    -> END    -> //Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> CALL simpleproc(@a);Query OK, 0 rows affected (0.00 sec) mysql> SELECT @a;+------+| @a   |+------+| 3    |+------+1 row in set (0.00 sec)
Copy after login

茅塞顿开,谢谢版主

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template