PHP数组作为输入到存储过程 [英] PHP Array as input to Stored Procedure

查看:127
本文介绍了PHP数组作为输入到存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我现在做: - 在PHP

This is what i am doing now : - in PHP

foreach($array as $value)
{
$query = select abc from tblname where colname =" .$value.
// fire query
}

然后我创建这些值数组并相应地显示。

then i create array of these values and display accordingly.

问题: -

我已经申请的foreach,其中每一个遇到数组中的值时触发查询。
因此,如果我在阵列有10个值,它触发10查询。并使用网络的10倍,导致缓慢的输出。

I have applied foreach, which fires the query every time it encounters a value in the array. result, if i have 10 values in my array it fires 10 queries. and uses network 10 times, result slow output.

我想要的 -

我想给数组存储过程,它应给我一个结果,这将有相应的阵列中的所有元素的输出。

I want to give the array to a stored procedure which shall give me a resultset which will have the outputs corresponding to all the elements in the array.

我知道这是可以做到的,但不知道怎么办。
MySQL的多年平均值采取数组作为数据类型。

I know this can be done but do not know how. the mysql doesnot take arrays as datatype.

的结果应该是该网络应只使用一次,任何数量值的despit阵列中的

the result shall be that network shall be used only once, despit of any number of values in the array.

LIKE -

StoredProcedure(inputMysqlARRAY)   // not possible, need a workaroung
{
// fire simple select(same) query for each value.
}

然后从PHP和输入数组调用此存储过程。 //需要的解决方法。

then call this stored procedure from PHP and input array. // need workaround.

推荐答案

您只需要对你的通话更聪明。例如,保持高速缓存数据库对象周围,诸如此类的事情。

You just have to be smarter about your calls. For instance, keeping cached DB objects around and that sort of thing.

不知道更多关于你的code(你的问题是相当乱码),看来,如果您的查询是这样的:

Without knowing more about your code (your question is fairly garbled), it seems that if your query is something like this:

$query = "select abc from tblname where colname =" .$value; // run 10 times.

您真的只需要编写聪明code:

You really just need to write smarter code:

$values = array(); // Now, populate this array.  
                   // When you're done, run the query:
$query = 'select abc from tblname where colname IN (\''.implode('\',\'', $values).'\')';

一般情况下,我们称此为动态SQL,是怎么会事通常是今天做的基础。存储过程(或者,根据我怎么看你的问题,存储功能)有时是有用的,但在一定程度过时作为一阶方法与SQL接口。该DB家伙仍然有时它发誓,但我认为,即使他们是相当不错的共识是更智能的查询总是更好的。

Generally, we refer to this as Dynamic SQL and is the underpinning for how things are typically done today. A stored procedure (or, based on how I read your question, stored function) is useful at times, but is somewhat antiquated as a first-order methodology for interfacing with SQL. The DB guys still sometimes swear by it, but I think that even they are fairly well in consensus that smarter queries are always better.

这篇关于PHP数组作为输入到存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆