Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to write a function that selects contents from a database. The problem I have is a lack of understanding of arrays and returning variables from functions.

I've written the function but seem to be going round in circles as to how to build the array and then return it from the function.php.

Main php file. (Labels and names have been changed to protect the innocent.)

<?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'];
?>

The Function.php file

<?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?
    }
?>

The construction of the sql works, but I believe dynamic variables are created by using $$label, which declares the new variable as the content of the old one. Does this apply to arrays as well? I'm building a web application that will be doing a lot of database queries and I wanted to shorten the repetition of code.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can just return array in Function.php(By adding return statement in last line as):

return $temp;

So retrieve the returned value in the calling function,

$tablex=sbpolldb ("users",$fields, null, 1, null); 
echo $tablex['row_name'];
share|improve this answer
    
cheers thank you, Acharya –  Richard Banton May 9 '13 at 9:37
    
No Problem .. . –  progrrammer May 9 '13 at 12:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.