-1

I want to implement the following code via array:

if ($z=='one'){$result = mysql_query( " SELECT * FROM table1", $link);}
if ($z=='two'){$result = mysql_query( " SELECT * FROM table2", $link);}

My try is here: (full code)

$z = $_GET["z"];

$link = mysql_connect("localhost","root","");
mysql_select_db("DBname",$link);


function one(){
$result = mysql_query( " SELECT * FROM table1");
return $result;
 }

function two(){
$result = mysql_query( " SELECT * FROM table2");
return $result;
 }

$arr=array ('one'=>"one",'two'=>"two");
$result=$arr[$z]();


while($end = mysql_fetch_assoc($result))    {
$end["col1"];
$end["col2"];
$end["col3"];
 }

mysql_close($link);

Why this code does not work ??

Tnx friends

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
  • Variable functions are neat, though probably very unnecessary here. If you were to write `var_dump($_GET);` right at the start, what results do you get? – Muhammad Abdul-Rahim May 11 '15 at 15:09
  • It does not matter - any function not implemented – Shafizadeh May 11 '15 at 15:11
  • 2
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 11 '15 at 15:12

2 Answers2

0
$z = $_GET["z"];

$link = mysql_connect("localhost","root","");
mysql_select_db("DBname",$link);


function one(){
   $result = mysql_query( " SELECT * FROM table1");
   return $result;
}

function two(){
    $result = mysql_query( " SELECT * FROM table2");
    return $result;
}

$arr=array ('one'=>"one",'two'=>"two");
$result=$arr[$z]();


while($end = mysql_fetch_assoc($result))    {
    echo $end["col1"];
    echo $end["col2"];
    echo $end["col3"];
}

mysql_close($link);
user3419778
  • 856
  • 3
  • 8
  • 11
0

The problem is that the content of your $arr is not the function you defined with the same name (why would it be?). It looks like you are trying to use a closure

$arr['one'] = function() {
    $result = mysql_query( " SELECT * FROM table1");
    return $result;
};

$arr['two'] = function() {
    $result = mysql_query( " SELECT * FROM table2");
    return $result;
};

then to call those you can use your existing code without any need for the if (even faster then checking for the value):

$result = $arr[$z];

you can also use call_user_func_array(), and keep your existing function definitions. This seems like what you were initially trying to achieve.

$arr = array ('one'=>"one",'two'=>"two");
if (isset($arr[$z])) $result = call_user_func_array($z,array());
  • If i want to use foreach and two array (arr,arr2),so i think to using if statement is more optimized. My target was optimization, for this reason i used array instead if statement. anyway tnx – Shafizadeh May 11 '15 at 18:11
  • Oh I see, I thought you wanted to execute each one of them. You don't even need to use if, you can call directly the right function. if optimization is your goal, this way is even faster than having to verify the content of `$z`. – Félix Adriyel Gagnon-Grenier May 11 '15 at 18:17
  • "if optimization is your goal, this way is even faster than having to verify the content of $z". 'this way' is which one ? how i can call directly the right function ? – Shafizadeh May 11 '15 at 18:26
  • at this line: `$result = $arr[$z];`. since the functions are defined under the key of their name (`one` or `two`) this line will call the relevant function, without having to verify which one it is – Félix Adriyel Gagnon-Grenier May 11 '15 at 18:27
  • also using `if (isset($arr[$z])) $result = call_user_func_array($z,array());` the if isset() is some extra white-listing of your function to be sure no malicious code could call an undefined function, but that will call the function named after the content of `$z` – Félix Adriyel Gagnon-Grenier May 11 '15 at 18:28