PHP:调用自定义php函数时的奇怪行为

时间:2009-10-20 06:23:22

标签: php

我在使用Flex编写PHP代码时遇到了奇怪的行为。让我解释一下情况:

我有两个功能可以说:

populateTable() //将一些数据放入使用flex制作的表格中 createXML() //创建一个xml文件,Fusion Charts使用该文件创建图表

现在,如果我单独调用 populateTable(),表格会填充数据,但是如果我用 createXML()调用它,则表格不会被填充但 createXML()是否有效,即创建一个xml文件。

即使我运行以下代码,只生成xml文件,但表保持为空,而我在createXML()之前调用populateTable()。知道可能出现什么问题吗?

MXML部分

<mx:HTTPService id="userRequest" url="request.php" method="POST"  resultFormat="e4x">
    <mx:request xmlns="">
        <getResult>send</getResult>
    </mx:request>

<mx:DataGrid id="dgUserRequest" dataProvider="{userRequest.lastResult.user}" x="28.5" y="36" width="525" height="250" >
<mx:columns>
    <mx:DataGridColumn headerText="No." dataField="no" />
    <mx:DataGridColumn headerText="Name" dataField="name"/>
    <mx:DataGridColumn headerText="Age" dataField="age"/>
</mx:columns>

PHP部分

<?php
//--------------------------------------------------------------------------
function initialize($username,$password,$database)
//--------------------------------------------------------------------------
{

# Connect to the database
$link = mysql_connect("localhost", $username,$password);
if (!$link) 
{
    die('Could not connected to the database : ' . mysql_error());
}
# Select the database
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) 
{
    die ('Could not select the DB : ' . mysql_error());
}
//

populateTable();
createXML();



# Close database connection

}

//--------------------------------------------------------------------------
populateTable()
//--------------------------------------------------------------------------
{   
if($_POST['getResult'] == 'send')
{

   $Result = mysql_query("SELECT * FROM session" );
   $Return = "<Users>";
   $no = 1;
   while ( $row = mysql_fetch_object( $Result ) )
   {
   $Return .= "<user><no>".$no."</no><name>".$row->name."</name><age>".$row->age."</age><salary>". $row->salary."</salary></session>";
     $no=$no+1; 
   $Return .= "</Users>";
   mysql_free_result( $Result );
   print ($Return);
}


//--------------------------------------------------------------------------
createXML()
//--------------------------------------------------------------------------
{   
$users=array
(
"0"=>array("",0),
"1"=>array("Obama",0),
"2"=>array("Zardari",0),
"3"=>array("Imran Khan",0),
"4"=>array("Ahmadenijad",0)
);
$selectedUsers=array(1,4); //this means only obama and ahmadenijad are selected and the xml file will contain info related to them only 

//Extracting salaries of selected users
$size=count($users);
for($i = 0; $i<$size; $i++)
{
  //initialize temp which will calculate total throughput for each protocol separately
  $salary = 0;
  $result = mysql_query("SELECT salary FROM userInfo where name='$users[$selectedUsers[$i]][0]'");
  $row = mysql_fetch_array($result))
  $salary = $row['salary'];
 }
  $users[$selectedUsers[$i]][1]=$salary;
}

//creating XML string

$chartContent = "<chart caption=\"Users Vs Salaries\" formatNumberScale=\"0\" pieSliceDepth=\"30\" startingAngle=\"125\">";
for($i=0;$i<$size;$i++)
{ 
 $chartContent .= "<set label=\"".$users[$selectedUsers[$i]][0]."\" value=\"".$users[$selectedUsers[$i]][1]."\"/>";
}
$chartContent .= "<styles>" .
                    "<definition>" .
                        "<style type=\"font\" name=\"CaptionFont\" size=\"16\" color=\"666666\"/>" .
                        "<style type=\"font\" name=\"SubCaptionFont\" bold=\"0\"/>" .
                    "</definition>" .
                    "<application>" .
                        "<apply toObject=\"caption\" styles=\"CaptionFont\"/>" .
                        "<apply toObject=\"SubCaption\" styles=\"SubCaptionFont\"/>" .
                    "</application>" .
                    "</styles>" .
        "</chart>";



$file_handle = fopen('ChartData.xml','w'); 
fwrite($file_handle,$chartContent);
fclose($file_handle);
}

initialize("root","","hiddenpeak");
?>

3 个答案:

答案 0 :(得分:0)

如果你的createXML函数在相同的页面生成上生成xml,并且它发送了一个Content-Type of application / xml,那么可能可能表格内容不能解析。同样,在您提供代码之前,这只会猜测。

答案 1 :(得分:0)

嗯,唯一的答案是createXML在某处覆盖/删除你的(可能是全局的)表。请再次查看您的代码。

答案 2 :(得分:0)

像上面给出的评论一样,如果没有更多的信息/代码/后端db:

,将会很困难

然而我的狂野客人:

populateTable()将数据插入db

由populateTable()插入的createXML()查询数据。

看起来像2 diff db conn。某些db要求你在代码中提交,如果是,请确保在关闭db conn之前在populateTable()中执行。