Let say that we are going to add some array to a multidimensional array. I can select the number of arrays which should be added to the main array but what about 99 array? Here is my code that I can change $howmany to add the number of arrays that I need (up to 4 arrays):
function DB_SINGLE_EXE($query, array $array_par, $howmany = 0 ,$column1 = "null", $column2 = "null", $column3 = "null", $column4 = "null"){
global $host;
global $dbname;
global $username;
global $password;
global $db;
try {
$stmt = $db->prepare($query);
$stmt->execute($array_par);
}catch(PDOException $e)
{
echo "ERROR: " . $query . "<br>" . $e->getMessage();
}
if ($howmany > 0){
$rowF = $stmt->fetchAll();
if($rowF){
$hell = count($rowF);
$stack = array(array("true" , $hell));
foreach ($rowF as $row) {
switch ($howmany) {
case 1:
array_push($stack, array($row[$column1]));
break;
case 2:
array_push($stack, array($row[$column1], $row[$column2]));
break;
case 3:
array_push($stack, array($row[$column1], $row[$column2], $row[$column3]));
break;
case 4:
array_push($stack, array($row[$column1], $row[$column2], $row[$column3], $row[$column4]));
break;
}
}
}else{
$stack = array(array("false" , "0"));
}
return $stack;
}
}
How I call the function:
$temp_query = "SELECT * FROM users WHERE status=:one";
$temp_array = array(":one" => "OK");
$results =DB_SINGLE_EXE($temp_query, $temp_array, 4, "user", "pw", "a_succ", "m_succ");
if ($results[0][0]="true"){
$username = $results[1][0];
$password = $results[1][1];
$suc = $results[1][2];
$m_suc = $results[1][3];
// Here I want to get more results but in function I just limited to 4 outputs. It might be 99 or even more and I don't want to use "select case" for 99 cases!
}
$column1,$column2, etc. variables coming from? Are they preset to some textual keys? Will they be preset all the way to 99? Or you just need to add all values from the$rowarray to the$stack?SELECT user, pw, a_succ, m_succ FROM .... Works faster and gives more understandable errors.