无法在同一个php页面中调用两次相同的函数

时间:2016-05-29 22:25:38

标签: php

我正在尝试在同一页面中调用我的函数selectuniversal();两次,但我只能从第一次调用中获得结果(如果我禁用第一次调用,则会获得结果)。

我的功能是: [func.php]

<?php
global $mycon;
$mycon = mysqli_connect('localhost','user','pass','db');
$mycon->set_charset('utf8');
class IDfun{

public function selectuniversal($tabela,$celula1,$celula2,$celulaordem,$ordem)
  {
    global $mycon;

    $IDquery = 'SELECT '.$celula1.','.$celula2.' FROM '.$tabela.' ORDER BY '.$celulaordem.' '.$ordem;
    $IDresultado= mysqli_query($mycon,$IDquery) or die(mysqli_error());

        do{
        $i=$row[$celula1];
        if($i>=1){echo '<option value='.$row[$celula1].'>'.$row[$celula2].'</option>';}
        } while ($row = mysqli_fetch_assoc($IDresultado));
    $mycon->close();
    }

}

?>

在我的[index.php]

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"></meta>
<?php 
include("func.php");
$IDfun=new IDfun;
?>

<meta charset="utf-8">
<title>Documento sem título</title>
</head>

<body>
<select id="cpais" name="cpais">
<?php $IDfun->selectuniversal('pais','pid','pnome','pnome','desc');?>
</select>

<p>

<select id="cvendid" name="cvendid">
<?php $IDfun->selectuniversal('vend','vid','vnome','vnome','desc');?>
</select>
</body>
</html>

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您正在关闭函数调用$mycon->close()中的mysqli连接,因此第二次调用该函数时,mysqli_query正在尝试使用已关闭的连接。

在您的函数中创建连接,或在php文件末尾调用函数外的$mycon->close()

相关问题