Heim  >  Artikel  >  Backend-Entwicklung  >  请大牛们给点 sql语句重用的办法?

请大牛们给点 sql语句重用的办法?

WBOY
WBOYOriginal
2016-06-23 14:05:27893Durchsuche

现在是我有几条非常长的sql语句,需要在不同地方分别应用,所以我想把它们分别写成存储过程来公用,也方便调试。
存储过程已经写好了,但是我用php调用的时候,每调用一个存储过程都要做一次新的数据连接,感觉太麻烦,太耗资源。
请问有没有更好的sql语句重用的方法,或是怎么处理一个php页面调用存储过程,而不关闭连接。谢谢!

现在实现方法如下:

$pdobakpro1 = new PDO('mysql:host=localhost;dbname=test','root','123456');
$pdobakpro2 = new PDO('mysql:host=localhost;dbname=test','root','123456');

    $sql1 = "call pro_1()";//一个存储过程一个数据库连接

    $stmt1 = $pdobakpro1->query($sql1);

    var_dump($stmt1);

$sql2 = "call pro_2()";//调第二个存储过程需建一个新的数据库连接

    $stmt2 = $pdobakpro2->query($sql2);

    var_dump($stmt2);


回复讨论(解决方案)

你 $stmt1 = $pdobakpro1->query($sql1); 后
把 $stmt1 的内容读出来就可以了


$pdobakpro1 = new PDO('mysql:host=localhost;dbname=test','root','123456');
$pdobakpro2 = new PDO('mysql:host=localhost;dbname=test','root','123456');

$pdobakpro1 = new PDO('mysql:host=localhost;dbname=test','root','123456');
$pdobakpro2 = clone $pdobakpro1;
是一样的
并没有“太耗资源”一说


$pdobakpro1 = new PDO('mysql:host=localhost;dbname=test','root','123456');
$pdobakpro2 = new PDO('mysql:host=localhost;dbname=test','root','123456');

$pdobakpro1 = new PDO('mysql:……

谢谢,好吧,明白版本大人的意思了。
我只想知道大家在用PHP调用存储过程是不是都要这样,一个存储过程连一次数据库。还是我操作错误。

一个存储过程可以返回多个结果集
我不明白的是,你为什么要把不相干的存储过程放在一起执行,而不是处理完一个在执行下一个
如果两个存储过程的结果想干,那么为什么不放在一个里面执行

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn