-1

The code below works fine when its not in wrapped in a function

$opt = 'logo_img';
$sql="SELECT option_value FROM r0_options WHERE option_name='".$opt."'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
var_dump($row);

However when I do as follows, and call the function, it gives NULL.

function get_result($opt){
    $sql="SELECT option_value FROM r0_options WHERE option_name='".$opt."'";
    $result=mysqli_query($db,$sql);
    $row=mysqli_fetch_array($result,MYSQLI_NUM);
    var_dump($row);
}

get_result('logo_img');
MWaheed
  • 306
  • 3
  • 9

2 Answers2

1

It's because you are not passing $db variable, either pass it to function or do the following:

function get_result($opt){
    global $db;

    $sql="SELECT option_value FROM r0_options WHERE option_name='".$opt."'";
    $result=mysqli_query($db,$sql);
    $row=mysqli_fetch_array($result,MYSQLI_NUM);
    var_dump($row);
}

get_result('logo_img');
divix
  • 1,265
  • 13
  • 27
0

You are forgotten to pass he '$db' to your function:

function get_result($db, $opt){

Franco
  • 2,309
  • 1
  • 11
  • 18