Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php存储过程问题

php存储过程问题

Jun 20, 2016 pm 12:38 PM


用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

茅塞顿开,谢谢版主

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles