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}."'"; // ' 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?
}
?>
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.