PHP中未定义的变量

时间:2010-10-11 15:09:36

标签: php mysql xml curl

我正在努力使用PHP中的cURL向URL发送XML请求。

首先,我只是想确保我的请求发送了正确的数据,所以这里是我代码中的一个片段。一旦我知道我正在发送正确的数据,我将在稍后添加curl语句。

到目前为止,这是我的代码:

$format = 'Y-m-j G:i:s'; 

$date = date ( $format );
$d = date ( $format, strtotime ( '-90 days' ) );

$sql = mysql_query("SELECT * FROM recurringPayments WHERE lastpmt <= '$d'");

$num_rows = mysql_num_rows($sql); 
echo $num_rows . " results found";

echo "<table style=\"border:1px solid green;\">
            <tr bgcolor=\"#bdd73b\">
            <th>ID</th>
            <th>Company Name</th>
            <th>Annual Subscription</th>
            <th>Package</th>
            <th>Cost</th>
            <th>Payer Ref</th>
            <th>Payment Ref</th>
            <th>Last Payment Date</th>
            </tr>";

            while ($row = mysql_fetch_array($sql))
            {
                echo "<tr>";
                echo "<td>" . $row['ID'] . "</td>";
                echo "<td>" . $row['compName'] . "</td>";
                echo "<td>" . $row['annualSub'] . "</td>";
                echo "<td>" . $row['package'] . "</td>";
                echo "<td>" . $row['cost'] . "</td>";               
                echo "<td>" . $row['payerref'] . "</td>";
                echo "<td>" . $row['pmtref'] . "</td>";
                echo "<td>" . $row['lastpmt'] . "</td>";
            }
            echo "</table>";        
while ($row = mysql_fetch_array($sql))
{
$xml_data ='<request type="receipt-in" timestamp="20030520151742">'.
       '<merchantid>test</merchantid>'.
           '<account>internet</account>'.
       '<orderid>transaction01</orderid>'.
       '<amount currency="EUR">'.$row['cost'].'</amount>'.
           '<payerref>'.$row['payerref'].'</payerref>'.
           '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
           '<autosettle flag="1" />'.
           '<md5hash />'.
       '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
       '</request>';
}

 echo $xml_data;

当我尝试回显$ xml_data时,收到以下错误消息:

  

注意:未定义的变量:xml_data   在C:\ wamp \ www \ Internal \ paymentDue.php中   第63行

我的逻辑是,当我输出表时,我能够输出XML,但是我可能(可能是)错了。任何指导都表示赞赏。

感谢。

PS:

我也发现,当我发布这个时,如果我在当前上下文中使用while循环,每次循环时都会覆盖$ xml_data。对此的任何帮助都会很棒。

5 个答案:

答案 0 :(得分:4)

你是从头到尾遍历结果集来构建html表,然后尝试再次为你的xml循环,即使你已经通过了集合的结尾。在同一个循环中构建。

在循环之前将xml_data设置为'',然后使用xml_data。=来构建它(甚至使用SimpleXML或XMLWriter而不是将其构建为字符串)

答案 1 :(得分:2)

试试这个:

   <?php

    $format = 'Y-m-j G:i:s'; 

    $date = date ( $format );
    $d = date ( $format, strtotime ( '-90 days' ) );

    $sql = mysql_query("SELECT * FROM recurringPayments WHERE lastpmt <= '$d'");

    $num_rows = mysql_num_rows($sql); 
    echo $num_rows . " results found";

    $xml_data = "";

    echo "<table style=\"border:1px solid green;\">
                <tr bgcolor=\"#bdd73b\">
                <th>ID</th>
                <th>Company Name</th>
                <th>Annual Subscription</th>
                <th>Package</th>
                <th>Cost</th>
                <th>Payer Ref</th>
                <th>Payment Ref</th>
                <th>Last Payment Date</th>
                </tr>";

                while ($row = mysql_fetch_array($sql))
                {
                    echo "<tr>";
                    echo "<td>" . $row['ID'] . "</td>";
                    echo "<td>" . $row['compName'] . "</td>";
                    echo "<td>" . $row['annualSub'] . "</td>";
                    echo "<td>" . $row['package'] . "</td>";
                    echo "<td>" . $row['cost'] . "</td>";               
                    echo "<td>" . $row['payerref'] . "</td>";
                    echo "<td>" . $row['pmtref'] . "</td>";
                    echo "<td>" . $row['lastpmt'] . "</td>";

                    $xml_data .='<request type="receipt-in" timestamp="20030520151742">'.
                           '<merchantid>test</merchantid>'.
                               '<account>internet</account>'.
                           '<orderid>transaction01</orderid>'.
                           '<amount currency="EUR">'.$row['cost'].'</amount>'.
                               '<payerref>'.$row['payerref'].'</payerref>'.
                               '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
                               '<autosettle flag="1" />'.
                               '<md5hash />'.
                           '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
                           '</request>';


                }
                echo "</table>";        


     echo $xml_data;

循环两次没有意义,循环导致光标移动到最后。这应该在初始循环期间构建您的xml_data变量。 另请注意,我已将$ xml_data声明为循环外的空字符串,每次循环时我只是使用。=

附加到字符串。

答案 2 :(得分:1)

你的第二个循环循环遍历已循环的mysql_query,因此循环指针位于末尾意味着此代码永远不会运行。由于它尚未运行,因此未设置$ xml_data,当您尝试在结束时回显它时,您会收到通知。

while ($row = mysql_fetch_array($sql))
{
$xml_data ='<request type="receipt-in" timestamp="20030520151742">'.
       '<merchantid>test</merchantid>'.
           '<account>internet</account>'.
       '<orderid>transaction01</orderid>'.
       '<amount currency="EUR">'.$row['cost'].'</amount>'.
           '<payerref>'.$row['payerref'].'</payerref>'.
           '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
           '<autosettle flag="1" />'.
           '<md5hash />'.
       '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
       '</request>';
}

尝试添加

mysql_data_seek($sql, 0);
在第二次循环之前

答案 3 :(得分:0)

当您为初始打印进行迭代时,您正在向前移动结果集点。当您想要输出XML时,指针就像结果集的末尾一样,因此您需要重置它或在打印时构建XML。

对于后者,试试这个:

$xmlData = '';
while ($row = mysql_fetch_array($sql))
{
    echo "<tr>";
    echo "<td>" . $row['ID'] . "</td>";
    echo "<td>" . $row['compName'] . "</td>";
    echo "<td>" . $row['annualSub'] . "</td>";
    echo "<td>" . $row['package'] . "</td>";
    echo "<td>" . $row['cost'] . "</td>";               
    echo "<td>" . $row['payerref'] . "</td>";
    echo "<td>" . $row['pmtref'] . "</td>";
    echo "<td>" . $row['lastpmt'] . "</td>";

    $xmlData .= '<request type="receipt-in" timestamp="20030520151742">'.
   '<merchantid>test</merchantid>'.
       '<account>internet</account>'.
   '<orderid>transaction01</orderid>'.
   '<amount currency="EUR">'.$row['cost'].'</amount>'.
       '<payerref>'.$row['payerref'].'</payerref>'.
       '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
       '<autosettle flag="1" />'.
       '<md5hash />'.
   '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
   '</request>';
}

echo "</table>";

答案 4 :(得分:0)

$format = 'Y-m-j G:i:s'; 

$date = date ( $format );
$d = date ( $format, strtotime ( '-90 days' ) );

$sql = mysql_query("SELECT * FROM recurringPayments WHERE lastpmt <= '$d'");

$num_rows = mysql_num_rows($sql); 
echo $num_rows . " results found";

$xml_data = ''; // init empty

echo "<table style=\"border:1px solid green;\">
            <tr bgcolor=\"#bdd73b\">
            <th>ID</th>
            <th>Company Name</th>
            <th>Annual Subscription</th>
            <th>Package</th>
            <th>Cost</th>
            <th>Payer Ref</th>
            <th>Payment Ref</th>
            <th>Last Payment Date</th>
            </tr>";

while ($row = mysql_fetch_array($sql))
{
   echo "<tr>";
   echo "<td>" . $row['ID'] . "</td>";
   echo "<td>" . $row['compName'] . "</td>";
   echo "<td>" . $row['annualSub'] . "</td>";
   echo "<td>" . $row['package'] . "</td>";
   echo "<td>" . $row['cost'] . "</td>";               
   echo "<td>" . $row['payerref'] . "</td>";
   echo "<td>" . $row['pmtref'] . "</td>";
   echo "<td>" . $row['lastpmt'] . "</td>";

   $xml_data .='<request type="receipt-in" timestamp="20030520151742">'.
                       '<merchantid>test</merchantid>'.
                           '<account>internet</account>'.
                       '<orderid>transaction01</orderid>'.
                       '<amount currency="EUR">'.$row['cost'].'</amount>'.
                           '<payerref>'.$row['payerref'].'</payerref>'.
                           '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
                           '<autosettle flag="1" />'.
                           '<md5hash />'.
                       '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
                       '</request>';

}
echo "</table>";        

echo $xml_data;