0

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

我已经编写了函数,但似乎围绕如何构建数组然后从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 的构造是有效的,但我相信动态变量是通过 using 创建的$$label,它将新变量声明为旧变量的内容。这也适用于数组吗?我正在构建一个将执行大量数据库查询的 Web 应用程序,并且我想缩短代码的重复性。

4

1 回答 1

2

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

return $temp;

所以在调用函数中检索返回值,

$tablex=sbpolldb ("users",$fields, null, 1, null); 
echo $tablex['row_name'];
于 2013-05-08T16:44:00.073 回答