dongpa3109 2013-12-14 17:27
浏览 16

第34行php上的未定义变量

My code:

<?php
 include("funcoes.php");

?>

<HTML>
<HEAD>
  <META charset = "UTF-8">

  <TITLE>Menu</TITLE>
</HEAD>
<BODY>

<center>
  <form method="POST" >
    Marca: <input type="text" name="marca"></a><BR>
    Preço: <input type="number" name="preco"></a><BR>
    Polegadas: <input type="number" name="tamanho"></a><BR>
    Tipo de Tela: <select name="tipoTela">
    <option value="1"> LED</option>
    <option value="2"> LCD</option>
    <option value="3"> Plasma</option>
    <option value="4"> CRT</option>
    <option value="5"> DLP</option>
  </select> <BR><BR>
    Formato:     <select name="formato">
    <option value="1"> Convencional</option>
    <option value="2"> Widescreen(HD)</option>
  </select> <BR><BR>
    Fabricante: <select name="fornecedor">
    <? $fornecedores = buscarFornecedores();
                    foreach ($fornecedores as $fornecedores) { ?>
    <option value="<? echo $fornecedores; ?>">
      <?php echo $fornecedores; ?> //linha 34
    </option>
    <? } ?>
  </select>
  </form>
  <center>
</BODY>
</HTML>

The function:

function buscarFornecedores (){
    echo "oi1";
    $qry = "SELECT * FROM public.fornecedor";
    $result = pg_query($qry) or die("Cannot execute query: $qry
");

    while($row = pg_fetch_object($result)){

        $fornecedores[] = $row;
    }

    return $fornecedores;
}

The error is this:

Undefined variable: fornecedores in /var/www/cadastro.php on line 34

The problem is that in a computer of a friend, the same code works normally, with the same stuffs.

  • 写回答

3条回答 默认 最新

  • duandou8120 2013-12-14 17:29
    关注

    You need to define the variable as an array before you start adding items to it:

    function buscarFornecedores (){
        $fornecedores = array();
        ....
    

    In PHP a variable will only be scoped to the function it is defined inside unless you use the global keyword. More info on that here

    Seeing as you were trying to assign $fornecedores from the returned value of the function you would also have needed to define it anyway, in the global scope, before adding items to it as an array.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?