Jump to content

Recommended Posts

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

Edited by ldiaz
Link to comment
https://forums.phpfreaks.com/topic/270035-2-bucle-while/
Share on other sites

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

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.

Edited by Psycho
Link to comment
https://forums.phpfreaks.com/topic/270035-2-bucle-while/#findComment-1388476
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.