查询返回数组 [英] Query returning array
问题描述
我错过了一些非常明显的事情,但现在已经很晚了,我已经很长时间盯着它了。
TIA for回复
我正在编写一个基本函数(在本文的底部列出)
将查询中的数据返回到数组中。
意图是以下代码:
$ foo = dbSelectData(" SELECT foo,bar FROM table",$ link);
将返回使用键的数组:''foo''和''bar''。
但我得到的是一个多维数组。
执行var_dump on $ foo原来是
array(2){
[" foo"] =>
string(5)" ; Stuff"
[" bar"] =>
string(10)" More Stuff"
}
我想知道我在编写下面的函数时出错了。
函数dbSelectData($ query,$ connection,$ rtype = MYSQL_ASSOC){
$ result = my sql_query($ query,$ connection);
if(!$ result){
dbThrowError("函数dbSelectData中的错误。查询是< em>
$查询< / em>。");
返回FALSE;
}
else {
$ numrows = mysql_num_rows($ result);
if($ numrows == 0){
返回FALSE;
}
else {
while($ rows = mysql_fetch_array($ result,$ rtype)){
$ output [] = $ rows;
}
mysql_free_result($ result);
返回$ output;
}
}
}
提前致谢。
-
Karl Groves
I''m missing something very obvious, but it is getting late and I''ve
stared at it too long.
TIA for responses
I am writing a basic function (listed at the bottom of this post) that
returns data from a query into an array.
The intent is that the following code:
$foo = dbSelectData("SELECT foo, bar FROM table", $link);
would return an array with the keys: ''foo'' and ''bar''.
But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
I''m wondering where I went wrong in writing the function below.
function dbSelectData($query, $connection, $rtype=MYSQL_ASSOC){
$result = mysql_query($query, $connection);
if(!$result){
dbThrowError("Error in function dbSelectData. Query Was <em>
$query</em>.");
return FALSE;
}
else{
$numrows = mysql_num_rows($result);
if($numrows == 0){
return FALSE;
}
else{
while($rows = mysql_fetch_array($result, $rtype)){
$output[] = $rows;
}
mysql_free_result($result);
return $output;
}
}
}
Thanks in advance.
--
Karl Groves
推荐答案
foo = dbSelectData(" SELECT foo,bar FROM table",
foo = dbSelectData("SELECT foo, bar FROM table",
link);
会返回一个包含键的数组:''foo''和''bar''。
但我得到的是一个多维数组。
在
link);
would return an array with the keys: ''foo'' and ''bar''.
But what I get is instead a multidimensional array.
Doing a var_dump on
foo上做一个var_dump原来是
数组(2){
[" foo"] =>
st ring(5)" Stuff"
[" bar"] =>
string(10)" More Stuff"
}
我想知道我在编写下面的函数时出了什么问题。
函数dbSelectData(
foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
I''m wondering where I went wrong in writing the function below.
function dbSelectData(
这篇关于查询返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!