欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2022-06-05 13:18:11
...
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>

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