Php 在函数中创建动态数组并从返回数组
我正在尝试编写一个从数据库中选择内容的函数。我的问题是缺乏对数组和函数返回变量的理解 我已经编写了这个函数,但似乎在讨论如何构建数组,然后从function.php返回它 主php文件。(标签和名称已更改,以保护无辜者。)Php 在函数中创建动态数组并从返回数组,php,return-value,dynamic-arrays,Php,Return Value,Dynamic Arrays,我正在尝试编写一个从数据库中选择内容的函数。我的问题是缺乏对数组和函数返回变量的理解 我已经编写了这个函数,但似乎在讨论如何构建数组,然后从function.php返回它 主php文件。(标签和名称已更改,以保护无辜者。) 您只需在Function.php中返回数组(通过在最后一行添加return语句,如下所示): 因此,在调用函数中检索返回值 $tablex=sbpolldb ("users",$fields, null, 1, null); echo $tablex['row_name']
您只需在Function.php中返回数组(通过在最后一行添加return语句,如下所示):
因此,在调用函数中检索返回值
$tablex=sbpolldb ("users",$fields, null, 1, null);
echo $tablex['row_name'];
<?php
function sbpolldb($sbbeta, $sbbecon, $sbbeorder, $sbbelimit, $sbbegroup){ //sbbeta = table name, sbbecon = where condition, sbbeval = Where value, sbbesort = sort value, sbbelim -
include '../sb_mysqli_connect.php';//Connect to the Database
if (empty($sbbeorder)) {
$sbbeotemp="";
} else {
$sbbeotemp=" ORDER By ".$sbbeorder;
} //Check if order by is null
if (empty($sbbelimit)){
$sbbeltemp="";
} else {
$sbbeltemp=" LIMIT ".$sbbelimit;
} //Check if there's a Limit set
if (empty($sbbegroup)){
$sbbegtemp="";
} else {//$ssbegtemp=' GROUP By '.$sbbegroup;
$count = sizeof($sbbegroup);
$sbbegtemp = " GROUP BY ";
//Loop to create WHERE conditons
for ($i = 0; $i < $count; $i++) {
$value = $sbbegroup[$i];
$sbbegtemp = $sbbegtemp.$value;
if ($i < ($count -1 )){
$sbbegtemp = $sbbegtemp.' , ';
}
};
}
if (empty($sbbecon)){
$sbbectemp='';
} else {
$count = sizeof($sbbecon);
$sbbectemp = 'WHERE ';
//Loop to create WHERE conditons
for ($i = 0; $i < $count; $i++) {
$value = $sbbecon[$i];
$sbbectemp = $sbbectemp.$value[0]." ="."'".$value{1}."'"; // ' is the code for an apostraphe
if ($i < ($count -1 )){
$sbbectemp = $sbbectemp.' AND ';
}
};
}
$sbbesql = "SELECT * FROM ".$sbbeta.' '
.$sbbectemp
.$sbbegtemp
.$sbbeotemp
.$sbbeltemp; //" is code for speach marks
mysql_select_db("database1");
$result = mysql_query($sbbesql, $sbbedbc) or die($sbbesql."<br/><br/>".mysql_error());;
$temp = mysql_fetch_array($result, MYSQL_ASSOC);
// Build Array Here Dynamically $sbbeta content as the variable name.
// How do I then Return this Array as the name is no longer $temp?
}
?>
return $temp;
$tablex=sbpolldb ("users",$fields, null, 1, null);
echo $tablex['row_name'];