简体   繁体   English

PHP无法将值插入数组

[英]PHP Cannot Insert Value Into Array

I have problem to set my array value with query result. 我在设置查询结果的数组值时遇到问题。 I have document table with ID_ATTACHMENT as primary key autoincreament, ID_TRANSACTION, DOCUMENT_NAME, NO_DOCUMENT, REMARKS, NAMA_FILE 我的文档表具有ID_ATTACHMENT作为主键自动隐藏功能,ID_TRANSACTION,DOCUMENT_NAME,NO_DOCUMENT,REMARKS,NAMA_FILE

$queryattacth = mysql_query("SELECT * FROM document A WHERE A.ID_TRANSACTION='13' ");
$tempAttc = array();
for ($i=1;$i<=15;$i++)
    {
        for ($j=0;$j<5;$j++)
            $tempAttc[$i][$j]="";
    }

while ($dataattach = mysql_fetch_array($queryattacth)){
        if (strtoupper($dataattach['DOCUMENT_NAME'])=='A')
        {
            $tempAttc[1][2]=$dataattach['NO_DOCUMENT'];
            $tempAttc[1][3]=$dataattach['REMARKS'];
            $tempAttc[1][4]=$dataattach['NAMA_FILE'];
        }
        else if (strtoupper($dataattach['DOCUMENT_NAME'])=='B'){
            $tempAttc[2][2]=$dataattach['NO_DOCUMENT'];
            $tempAttc[2][3]=$dataattach['REMARKS'];
            $tempAttc[2][4]=$dataattach['NAMA_FILE'];
        }
        else if (strtoupper($dataattach['DOCUMENT_NAME'])=='C'){
            $tempAttc[3][2]=$dataattach['NO_DOCUMENT'];
            $tempAttc[3][3]=$dataattach['REMARKS'];
            $tempAttc[3][4]=$dataattach['NAMA_FILE'];
        }
       .................
}

The result is doesn't show any value for this variable 结果是不显示此变量的任何值

$tempAttc[1][2];
$tempAttc[1][3];
$tempAttc[1][4];

but have result for this variable 但是有这个变量的结果

 $tempAttc[2][2];
 $tempAttc[2][3];
 $tempAttc[2][4];

Anyone can hep me about why this happened? 任何人都可以帮助我了解为什么会这样吗?

I just have try this and it solved my problem 我只是尝试了一下,它解决了我的问题

$tempAttc[1][2]=$tempAttc[1][2].''.$dataattach['NO_DOCUMENT'];
$tempAttc[1][3]=$tempAttc[1][3].''.$dataattach['REMARKS'];
$tempAttc[1][4]=$tempAttc[1][4].''.$dataattach['NAMA_FILE'];

But I still don't understand about my problem, maybe anyone can explain why those combination not working for me... 但是我仍然不了解我的问题,也许任何人都可以解释为什么这些组合对我不起作用...

First RED ALERT : mysql_ functions are deprecated... they are on the cutting room floor and about to be swept away. 第一个RED ALERT :不推荐使用mysql_函数...它们位于mysql_地板上,即将被清除。 Take a look into PDO or mysqli_ . 看一下PDOmysqli_ That said, I have kept the deprecated functions in this answer. 就是说,我在这个答案中保留了不推荐使用的功能。

In the code below, I have added comments on why I did things the way I did. 在下面的代码中,我添加了有关为什么以这种方式进行操作的注释。

//Assuming that DOCUMENT_NAME goes from A to the 15th letter of
//the alphabet, order by it to make things easier.
$queryattacth = mysql_query("SELECT * 
                             FROM document A 
                             WHERE A.ID_TRANSACTION='13'
                             ORDER BY DOCUMENT_NAME");

//Initialize $tempAttc as an empty array (to be fully populated by the query)
$tempAttc = array();

//Use MYSQL_NUM to get an array without the associative (text) indices.
while ($dataattach = mysql_fetch_array($queryattacth, MYSQL_NUM)) {
  //Assuming the columns are exactly ordered as you have listed, push
  //the results into the array (will include all columns, not just 2-4)
  $tempAttc[] = $dataattach;
}

That should do what you want up to the part about inserting into a PDF (which is not part of your question). 在插入PDF之前,这应该做您想要的事情(这不是问题的一部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM