ldiaz Posted October 29, 2012 Share Posted October 29, 2012 Hello all, I have a problem with a while bucle and an array, here is the code: I have 2 querys, the result of query 1 is stored into an array and the second query is handled with while. The problem is the $bib variable have always NULL as value, but it's wrong, ... $sql="select distinct(biblionumber) from biblioitems where biblionumber in(select biblionumber from issn_master where issn1 in($lista)or issn2 in($lista) or issnL in ($lista))"; $qry = mysql_query($sql); $sql1="select embargo,fondo,tipo_fondo from stage1 where grupo='".$grupo."' and issn in(select issn1 from issn_master where issn1 in($lista)) or issn in(select issn2 from issn_master where issn2 in($lista));"; $qry1 = mysql_query($sql1); $data = array(); while ($row = mysql_fetch_assoc($qry)) { $data[] = $row[biblionumber]."\n"; } $bib=implode("\n",$row[biblionumber]); while($row1 = mysql_fetch_assoc($qry1)) { $inc++; $sql_item= "insert into items (biblionumber,biblioitemnumber,itype,enumchron,booksellerid,itemcallnumber,barcode,homebranch,holdingbranch,notforloan,damaged,itemlost,wthdrawn,timestamp)values('".$bib."','".$bib."','".$row1[tipo]."','".$row1[fondo]."','".$row1[embargo]."','".$grupo."-".$inc.$row1['type']."-".$bib."','".$grupo."-".$inc.$row1['type']."-".$bib."','".$grupo."','".$grupo."',0,0,0,0,'".$date."')"; echo $sql_item; } If you execute this: foreach($data as $bib){ echo $bib; } Then the result is: 21758 68314 I don't see what is wrong in my code. The correct result expected is: insert into items (biblionumber,biblioitemnumber,itype,enumchron,booksellerid,itemcallnumber,barcode,homebranch,holdingbranch,notforloan,damaged,itemlost,wthdrawn,timestamp)values(21758'',21758'',21758'','1996-','','MHUPA-1-21758','MHUPA-1-21758','MHUPA','MHUPA',0,0,0,0,'')insert into items (biblionumber,biblioitemnumber,itype,enumchron,booksellerid,itemcallnumber,barcode,homebranch,holdingbranch,notforloan,damaged,itemlost,wthdrawn,timestamp)values(68314'',68314'','68314','2001-','','MHUPA-2-68314','MHUPA-2-68314','MHUPA','MHUPA',0,0,0,0,'') Some on can help? Thanks a lot in advance Link to comment https://forums.phpfreaks.com/topic/270035-2-bucle-while/ Share on other sites More sharing options...
DavidAM Posted October 29, 2012 Share Posted October 29, 2012 1 - Turn on error reporting. Line 16 ($inc++) should be throwing an error unless you have it defined somewhere else. 2 - Initialize $inc before the while loop: $inc = 0; 3 - Why are you appending a newline onto the biblionumber and then imploding with another newline? That will put two newlines between each number, is this what you want? I would just do the implode with two newlines if that's what you want. Although the way you have it, there will be a newline after the last number, that my suggestion will not include. 4 - The implode is accessing $row, which is not an array at this point, so this line (#13) is throwing an error as well. I think you want $data here. At this point (after line 13), $bib is undefined, so it is useless to put it into a query. #4 ADDRESSES YOUR PROBLEM STATEMENT Link to comment https://forums.phpfreaks.com/topic/270035-2-bucle-while/#findComment-1388468 Share on other sites More sharing options...
Psycho Posted October 29, 2012 Share Posted October 29, 2012 I see a few problems. The biggest is you should NEVER run queries in loops. 99% of the time there is a more efficient way to achive what you need and in the other 1% of cases you are probably doing something you shouldn't be. So, you have this loop: while ($row = mysql_fetch_assoc($qry)) { $data[] = $row[biblionumber]."\n"; } And after that you have this line $bib=implode("\n",$row[biblionumber]); It makes no sense to try and implode $row[biblionumber] since it will only contain the value from the last record. I think you meant to implode $data. Even so, why are you imploding data to put into a field in the DB? That's typically a very bad design that is going to create problems for you later on. I also see references to fields from the 2nd SELECT query in the INSERT query that are not part of the result set (e.g. 'tipo' vs. 'tipo_fondo'). Anyway, using the bad design, you can insert all the record you want in a single query. (Although I highly encourage you to rethink this design!). I would provide the query, but, really, what I see in the code makes no sense. You are inserting $bib (an imploded list) into several fields. Also, the incrementing $inc doesn't have a purpose. I could create a single query, but the $inc makes it a little difficult that it would take more of my time than I am willing to invest. Link to comment https://forums.phpfreaks.com/topic/270035-2-bucle-while/#findComment-1388476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.