Home php教程 php手册 璇﹁Вphp璋冪敤MySQL瀛樺偍杩囩▼鏂规硶_PHP鏁欑▼ | 甯涔嬪

璇﹁Вphp璋冪敤MySQL瀛樺偍杩囩▼鏂规硶_PHP鏁欑▼ | 甯涔嬪

Jun 13, 2016 am 10:09 AM
mysql php

php涓巑ysql鏄ぉ鐢熺殑涓€瀵癸紝涓嬮潰鎴戞潵浠嬬粛鍦╬hp涓€庝箞璋冪敤mysql瀛樺偍杩囩▼骞舵墽琛岃繑鍥炵粨鏋滐紝涓嬮潰鎴戞€荤粨浜嗕竴浜涘父鐢ㄧ殑php璋冪敤MySQL瀛樺偍杩囩▼鐨勬柟娉曪紝鏈夐渶瑕佷簡瑙g殑鍚屽鍙弬鑰冦€?p>MySQL浠?.0寮€濮嬫墠寮曞叆瀛樺偍杩囩▼锛屽弽姝d互鍓嶅仛搴旂敤鐨勬椂鍊欎粠娌$杩囷紝涓嶈繃鐜板湪鍥犱负涓昏浣滃唴閮ㄧ郴缁燂紝鎵€浠ュ緢澶氬簲鐢ㄩ兘鐢ㄥ埌浜嗗瓨鍌ㄨ繃绋嬶紝褰撶劧鍓嶅彴鏈夋椂鍊欎篃闇€瑕佽皟鐢∕ySQL瀛樺偍杩囩▼锛孭HP鐨凪ySQL Lib濂藉儚鏀寔鐨勪笉鏄緢濂斤紝涓嶈繃鎴戞悳绱簡浜涜祫鏂欙紝铏界劧涓嶅锛屼絾鏄繕鏄皾璇曠殑浣跨敤浜嗭紝鐜板湪浠嬬粛涓€涓嬫柟娉?/p>


1銆傝皟鐢ㄥ瓨鍌ㄨ繃绋嬬殑鏂规硶銆?/p>


a銆傚鏋滃瓨鍌ㄨ繃绋嬫湁 IN/INOUT鍙傛暟锛屽0鏄庝竴涓彉閲忥紝杈撳叆鍙傛暟缁欏瓨鍌ㄨ繃绋嬶紝璇ュ彉閲忔槸涓€瀵癸紝涓€涓猵hp鍙橀噺锛堜篃鍙互涓嶅繀锛屽彧鏄病鏈塸hp鍙橀噺鏃讹紝娌℃湁鍔炴硶杩涜鍔ㄦ€佽緭鍏ワ級锛屼竴涓狹ysql鍙橀噺銆?/p>


b銆傚鏋滃瓨鍌ㄨ繃绋嬫湁OUT鍙橀噺锛屽0鏄庝竴涓狹ysql鍙橀噺銆俶ysql鍙橀噺鐨勫0鏄庢瘮杈冪壒娈婏紝蹇呴』璁﹎ysql鏈嶅姟鍣ㄧ煡閬撴鍙橀噺鐨勫瓨鍦紝鍏跺疄涔熷氨鏄墽琛屼竴鏉ysql璇彞銆傚叆set @mysqlvar=$phpvar ;


c銆備娇鐢╩ysql_query()/mysql_db_query()鎵цmysql 鍙橀噺澹版槑璇彞銆?/p>

 

 浠g爜濡備笅 澶嶅埗浠g爜

mysql_query("set @mysqlvar銆?$pbpvar銆?quot;);


杩欐牱锛屽湪mysql鏈嶅姟鍣ㄩ噷闈㈠氨鏈変竴涓彉閲忥紝@mysqlar銆傚鏋滄椂IN鍙傛暟锛岄偅涔堝叾鍊煎彲浠ユ湁phpar浼犲叆銆?/p>


d銆?濡傛灉鏃跺瓨鍌ㄨ繃绋嬨€?/p>


1銆傛墽琛?call procedure锛堬級璇彞銆?/p>


涔熷氨鏄痬ysql_query("call proceduer([var1]...)");

 

2. 濡傛灉鏈夎繑鍥炲€硷紝鎵цselect @ar锛岃繑鍥炴墽琛岀粨鏋溿€?/p>

 

 浠g爜濡備笅 澶嶅埗浠g爜
mysql_query("select @var)"


鎺ヤ笅鏉ョ殑鎿嶄綔灏卞拰php鎵ц涓€鑸殑mysql璇彞涓€鏍蜂簡銆傚彲浠ラ€氳繃mydql_fetch_row()绛夊嚱鏁拌幏寰楃粨鏋溿€?/p>


涓嬮潰鎴戞€荤粨浜嗕竴浜涜皟鐢ㄥ瓨鍌ㄨ繃绋嬬殑瀹炰緥鏃犲弬鐨勫瓨鍌ㄨ繃绋?/p>

 浠g爜濡備笅 澶嶅埗浠g爜


$conn = mysql_connect('localhost','root','root') or die ("鏁版嵁杩炴帴閿欒!!!");
mysql_select_db('test',$conn);
$sql = "
create procedure myproce()
begin
INSERT INTO user (id, username, sex) VALUES (NULL, 's', '0');
end;
";
mysql_query($sql);//鍒涘缓涓€涓猰yproce鐨勫瓨鍌ㄨ繃绋?/p>

$sql = "call test.myproce();";
mysql_query($sql);//璋冪敤myproce鐨勫瓨鍌ㄨ繃绋嬶紝鍒欐暟鎹簱涓皢澧炲姞涓€鏉℃柊璁板綍銆?/p>

鍨嬩竴锛氳皟鐢ㄥ甫杈撳叆銆佽緭鍑虹被鍨嬪弬鏁扮殑鏂规硶

 浠g爜濡備笅 澶嶅埗浠g爜

$returnValue = '';
try {
 mysql_query ( "set @Return" );
 $spname = 'P__Test_GetInfo1';
 mysql_query ( "call $spname(@Return, '{$userId}', '{$pwd}')" ) or die ( "[$spname]Query failed:" . mysql_error () );
 $result_return = mysql_query ( "select @Return" );
 $row_return = mysql_fetch_row ( $result_return );
 $returnValue = $row_return [0];
} catch ( Exception $e ) {
 echo $e;
}
echo $returnValue; //杈撳嚭鏉ヨ嚜瀛樺偍杩囩▼涓緭鍑虹殑鍙橀噺

绫诲瀷浜岋細璋冪敤甯﹀涓緭鍑虹被鍨嬪拰澶氫釜杈撳叆绫诲瀷鍙傛暟鐨勬柟娉?/p>

 浠g爜濡備笅 澶嶅埗浠g爜

$userId = 0;
try{
    mysql_query("set @Message");
    mysql_query("set @Id");
    mysql_query("call P__Test_Login(@Message, @Id, '{$userId}', '{$pwd}')", $conn) or die("Query failed:".mysql_error());
    $result_mess = mysql_query("select @Message");
    $result_uid = mysql_query("select @Id");
    $row_mess = mysql_fetch_row($result_mess);
    $row_uid = mysql_fetch_row($result_uid);
    $Proc_Error = $row_mess[0];
    $uId = $row_uid[0];
}
catch( Exception $e )
{
   echo $e;
}
echo 'proc return message:'$Proc_Error.'
'; //杈撳嚭鏉ヨ嚜瀛樺偍杩囩▼涓緭鍑虹殑鍙橀噺
echo 'User id:'.$uId; //鑾峰彇鐢ㄦ埛id

绫诲瀷涓夛細璋冪敤甯﹁繑鍥炵粨鏋滈泦鐨勬柟娉?/p>

 浠g爜濡備笅 澶嶅埗浠g爜

try {
 $spname = 'P__Test_GetData';
 $query = mysql_query ( "call $spname()", $conn ) or die ( "[$spname]Query failed:".mysql_error() );
 while ( $row = mysql_fetch_array ( $query ) ) {
  echo $row ['ProvinceID'].'::'.$row ['ProvinceName']; //杈撳嚭鏁版嵁闆?br />  }

} catch ( Exception $e ) {
 echo $e;
}

绫诲瀷鍥涳細璋冪敤甯﹁繑鍥炲涓粨鏋滈泦鐨勬柟娉?鐩墠鍙兘閫氳繃mysqli鏉ュ疄鐜皛~)

 浠g爜濡備笅 澶嶅埗浠g爜

//PHP
$rows = array (); 
$db = new mysqli($server,$user,$psd,$dbname); 
if (mysqli_connect_errno()){ 
    $this->message('Can not connect to MySQL server'); 

$db->query("SET NAMES UTF8"); 
$db->query("SET @Message");
if($db->real_query("call P__Test_GetData2(@Message)")){ 
    do{ 
        if($result = $db->store_result()){ 
            while ($row = $result->fetch_assoc()){ 
                array_push($rows, $row); 
            } 
            $result->close(); 
        } 
    }while($db->next_result()); 

$db->close();
print_r($rows);
//Procedure
……
select * from T1 where ……
select * from T2 where ……
……

瀹炰緥鍥涳細浼犲嚭鍙傛暟鐨刬nout瀛樺偍杩囩▼

 浠g爜濡備笅 澶嶅埗浠g爜
$sql = "
create procedure myproce4(inout sexflag int)
begin
SELECT * FROM user WHERE sex = sexflag;
end;
";
mysql_query($sql);//鍒涘缓涓€涓猰yproce4鐨勫瓨鍌ㄨ繃绋?br /> $sql = "set @sexflag = 1";
mysql_query($sql);//璁剧疆鎬у埆鍙傛暟涓?
$sql = "call test.myproce4(@sexflag);";
mysql_query($sql);//璋冪敤myproce4鐨勫瓨鍌ㄨ繃绋?鍦╟md涓嬮潰鐪嬫晥鏋?/td>


瀹炰緥浜旓細浣跨敤鍙橀噺鐨勫瓨鍌ㄨ繃绋?/p>

 浠g爜濡備笅 澶嶅埗浠g爜
$sql = "
create procedure myproce5(in a int,in b int)
begin
declare s int default 0;
set s=a+b;
select s;
end;
";
mysql_query($sql);//鍒涘缓涓€涓猰yproce5鐨勫瓨鍌ㄨ繃绋?br /> $sql = "call test.myproce5(4,6);";
mysql_query($sql);//

璋冪敤myproce5鐨勫瓨鍌ㄨ繃绋?鍦╟md涓嬮潰鐪嬫晥鏋?/p>

瀹炰緥鍏細case璇硶

 浠g爜濡備笅 澶嶅埗浠g爜
$sql = "
create procedure myproce6(in score int)
begin
case score
when 60 then select '鍙婃牸';
when 80 then select '鍙婅壇濂?;
when 100 then select '浼樼';
else select '鏈煡鍒嗘暟';
end case;
end;
";
mysql_query($sql);//鍒涘缓涓€涓猰yproce6鐨勫瓨鍌ㄨ繃绋?br /> $sql = "call test.myproce6(100);";
mysql_query($sql);//

璋冪敤myproce6鐨勫瓨鍌ㄨ繃绋?鍦╟md涓嬮潰鐪嬫晥鏋?/p>

瀹炰緥涓冿細寰幆璇彞

 浠g爜濡備笅 澶嶅埗浠g爜
$sql = "
create procedure myproce7()
begin
declare i int default 0;
declare j int default 0;
while i set j=j+i;
set i=i+1;
end while;
select j;
end;
";
mysql_query($sql);//鍒涘缓涓€涓猰yproce7鐨勫瓨鍌ㄨ繃绋?br /> $sql = "call test.myproce7();";
mysql_query($sql);//

璋冪敤myproce7鐨勫瓨鍌ㄨ繃绋?鍦╟md涓嬮潰鐪嬫晥鏋?/p>


瀹炰緥鍏細repeat璇彞

 浠g爜濡備笅 澶嶅埗浠g爜
$sql = "
create procedure myproce8()
begin
declare i int default 0;
declare j int default 0;
repeat
set j=j+i;
set i=i+1;
until j>=10
end repeat;
select j;
end;
";
mysql_query($sql);//鍒涘缓涓€涓猰yproce8鐨勫瓨鍌ㄨ繃绋?br /> $sql = "call test.myproce8();";
mysql_query($sql);//

璋冪敤myproce8鐨勫瓨鍌ㄨ繃绋?鍦╟md涓嬮潰鐪嬫晥鏋?/p>

瀹炰緥涔濓細loop璇彞

 浠g爜濡備笅 澶嶅埗浠g爜

$sql = "
create procedure myproce9()
begin
declare i int default 0;
declare s int default 0;

loop_label:loop
set s=s+i;
set i=i+1;
if i>=5 then
leave loop_label;
end if;
end loop;
select s;
end;
";
mysql_query($sql);//鍒涘缓涓€涓猰yproce9鐨勫瓨鍌ㄨ繃绋?br /> $sql = "call test.myproce9();";
mysql_query($sql);//

璋冪敤myproce9鐨勫瓨鍌ㄨ繃绋?鍦╟md涓嬮潰鐪嬫晥鏋?/p>

瀹炰緥鍗侊細鍒犻櫎瀛樺偍杩囩▼

 浠g爜濡備笅 澶嶅埗浠g爜

mysql_query("drop procedure if exists myproce");//鍒犻櫎test鐨勫瓨鍌ㄨ繃绋?/p>

瀹炰緥鍗侊細瀛樺偍杩囩▼涓殑娓告爣

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles