创建动态数组并从函数中返回数组

时间:2013-05-08 16:35:40

标签: php return-value dynamic-arrays

我正在尝试编写一个从数据库中选择内容的函数。我遇到的问题是缺乏对数组的理解以及从函数中返回变量。

我已经编写了这个函数,但似乎围绕如何构建数组,然后从function.php返回它。

主要的php文件。 (标签和名称已经更改,以保护无辜者。)

<?php 
//include '../sb_mysqli_connect.php';//Connect to the Database
include 'functions.php' ; //Include the functions list

$username = 'foo';
$password = 'bar';


$fields = array( array('username', $username), array('password', $password)); //prep's     array for multiple where statements
sbpolldb ("users",$fields, null, 1, null); //function sbpolldb($sbbeta, $sbbecon, $sbbeorder, $sbbelimit, $sbbegroup)

echo $tablex['row_name_y'];
?>

Function.php文件

<?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}."'"; // &#39 is the code for an apostraphe
                if ($i < ($count -1 )){
                    $sbbectemp = $sbbectemp.' AND ';
                }
            };
        }
        $sbbesql = "SELECT * FROM ".$sbbeta.' ' 
        .$sbbectemp
        .$sbbegtemp
        .$sbbeotemp
        .$sbbeltemp; //&#34 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?
    }
?>

sql的构造有效,但我相信动态变量是使用$$label创建的,它将新变量声明为旧变量的内容。这也适用于数组吗?我正在构建一个Web应用程序,它将执行大量的数据库查询,我想缩短代码的重复次数。

1 个答案:

答案 0 :(得分:2)

你可以在Function.php中返回数组(通过在最后一行添加return语句):

return $temp;

因此,在调用函数中检索返回的值

$tablex=sbpolldb ("users",$fields, null, 1, null); 
echo $tablex['row_name'];