【问题标题】:php mysqli function not workingphp mysqli函数不起作用
【发布时间】:2014-12-29 06:51:51
【问题描述】:

我创建了一个不返回任何值的函数:

$j2_2=  mysqli_query($con, "select * from Hagrala where Ale = 7 order by Mispar desc limit 1;");
        $j2_3 = mysqli_fetch_array($j2_2);
        $spade_7_2=$j2_3['Mispar'];  

这行得通

$v='9';  
$j2_2=  mysqli_query($con, "select * from Hagrala where Ale = $v order by Mispar desc limit 1;");
        $j2_3 = mysqli_fetch_array($j2_2);
        $spade_9_2=$j2_3['Mispar'];     

这也有效
但是使用函数 foo 它不起作用:

function foo($arg_1)    

 {    
      $t_1 = mysqli_query($con, "select * from Hagrala where Ale = $arg_1 order by Mispar desc limit 1;");
      $t_2 = mysqli_fetch_array($t_1);
     return $t_2;  
 }      

 $spade_10_2=foo('10');  

有什么想法吗?

【问题讨论】:

    标签: php function mysqli


    【解决方案1】:

    $con 因作用域而对您的函数不可用。 $con 在全局范围内,除非您通过可用的方式使其对您的函数可用,否则它对您的函数不可用。最好的方法是将其作为参数传递给函数。

    function foo($arg_1, $con) {    
         $t_1 = mysqli_query($con, "select * from Hagrala where Ale = $arg_1 order by Mispar desc limit 1;");
         $t_2 = mysqli_fetch_array($t_1);
         return $t_2;  
    }      
    $spade_10_2=foo('10', $con);  
    

    【讨论】:

      【解决方案2】:

      在函数参数中传递$con 以在函数中访问它。

      function foo($arg_1,$con)    
      
       {    
            $t_1 = mysqli_query($con, "select * from Hagrala where Ale = $arg_1 order by Mispar desc limit 1;");
            $t_2 = mysqli_fetch_array($t_1);
           return $t_2;  
       }      
      
       $spade_10_2=foo('10');  
      

      【讨论】:

      • @DanielBen-Shabtay,如果它适合你。请标记它,以便对其他人也有用。
      猜你喜欢
      • 1970-01-01
      • 2017-10-20
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2013-03-24
      相关资源
      最近更新 更多