#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Ecuador
    Posts
    512
    Rep Power
    15

    (mysql) retrieving 1 row instead of 40 rows


    The following script only retrieves one row from my table where it should retrieve like 40 rows.

    PHP Code:
    <?
    $query 
    "SELECT Empresa, Paqnr, Codigo, Envio, Desde, Information, Comentario, Peso_Total, Valor, Peso FROM pongo ORDER BY `Empresa` DESC LIMIT 10"
    $result mysql_query($query);
    $num mysql_num_rows($result);

    if (
    $num != 0) {
     
    $filefopen("resulta.xml""w");
     
    $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
     
    $_xml .="<Caudat_xml>\r\n";
     if (
    $rows mysql_fetch_array($result)) {
     
    $_xml .="\t<num_paquete>=\"" $rows["Paqnr"] . "\">\r\n";$_xml .="\t</num_paquete>\r\n"
     
    $_xml .="\t<empresa>=\"" $rows["Empresa"] . "\">\r\n";$_xml .="\t</empresa>\r\n";
     
    $_xml .="\t<codigo>=\"" $rows["Codigo"] . "\">\r\n";$_xml .="\t</codigo>\r\n";
     
    $_xml .="\t<Envio>=\"" $rows["Envio"] . "\">\r\n";$_xml .="\t</Envio>\r\n";
     
    $_xml .="\t<desde>=\"" $rows["Desde"] . "\">\r\n";$_xml .="\t</desde>\r\n";
     
    $_xml .="\t<informacion>=\"" $rows["Paqnr"] . "\">\r\n";$_xml .="\t</informacion>\r\n";
     } else {
     
    $_xml .="\t<Query_Empty>=\"No Resulta\">\r\n";
     
    $_xml .="\t</Query_Empty>\r\n"; } }
     
    $_xml .="</Caudat_xml>";
     
    fwrite($file$_xml);
     
    fclose($file);
     echo 
    "XML formato list.  <a href=\"resulta.xml\">Por Aqui el formato XML.</a>";
     
    ?>
    the result is this

    Code:
    <?xml version="1.0" encoding="UTF-8" ?> 
    - <Caudat_xml>
      <num_paquete>="777000"></num_paquete> 
      <empresa>="mycom"></empresa> 
      <codigo>="899"></codigo> 
      <Envio>="to"></Envio> 
      <desde>="from"></desde> 
      <informacion>="777000"></informacion> 
      </Caudat_xml>
    Where did i go wrong?
    Last edited by ol4pr0; January 29th, 2004 at 11:50 AM.
  2. #2
  3. No Profile Picture
    Dissident
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2003
    Location
    New York
    Posts
    1,671
    Rep Power
    53
    You never loop over the record set.

    Change this:
    PHP Code:
    if ($rows mysql_fetch_array($result)) { 
    to

    PHP Code:
    while ($rows mysql_fetch_array($result) ) { 
    You'll also need to change your else statement to branch off of if( $num != 0 )
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Ecuador
    Posts
    512
    Rep Power
    15
    yea i tried the
    PHP Code:
    while ($row mysql_fetch_array($result)) 
    which gave me this error.

    Parse error: parse error, unexpected T_ELSE in xml.php on line 46

    thats prolly why you said to change the

    PHP Code:
    if ($num != 0
    THan in what should i change that
  6. #4
  7. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,939
    Rep Power
    902
    you will have to close the if loop before you have an else statement.

    PHP Code:
    <?
    if($num !=0)
    {
    while(
    $row mysql_fetch_array($result))
    {

    }else{
    //this is trying to use an "else" off of a "while" which you cannot do because the if has not yet been closed
    }
    }
    ?>
    PHP Code:
    <?
    if($num !=0)
    {
    while(
    $row mysql_fetch_array($result))
    {
    //code
    }
    }else{
    //this is properly aligned with the "if"
    }
    ?>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Ecuador
    Posts
    512
    Rep Power
    15
    Am i so blind that i cant see what you mean?

    i mean isnt that what i got?!
  10. #6
  11. No Profile Picture
    Dissident
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2003
    Location
    New York
    Posts
    1,671
    Rep Power
    53
    Indenting is your friend angrypanda

    INCORRECT:
    PHP Code:
    <?
    if($num !=0)
    {
        while(
    $row mysql_fetch_array($result))
        {

        }
        else
        {
            
    //this is trying to use an "else" off of a "while" which you cannot do because the if has not yet been closed
        
    }
    }
    ?>
    (you can't have an else statement branching off of a while)


    CORRECT:
    PHP Code:
    <?
    if($num !=0)
    {
        while(
    $row mysql_fetch_array($result))
        {
            
    //code
        
    }
    }
    else
    {
        
    //this is properly aligned with the "if"
    }
    ?>
    Draelon


    PHP Manual :: MySQL Manual :: How to Ask Questions the Smart Way
    =======================================================
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Ecuador
    Posts
    512
    Rep Power
    15
    Thanks alot drealon you got me on the right track.

    for all that whished to know and for the archives on this forum

    here it is..

    PHP Code:
    <?
    // db connections and all that 
    // and query :lol:


    $result mysql_query($query) or die (mysql_error()); 
    $num mysql_num_rows($result); 


    $filefopen("resulta.xml""w"); 
    $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
    $_xml .="<Caudat_xml>\r\n"
    if(
    mysql_num_rows($result) > 0){ 
    while(
    $rows mysql_fetch_array($result)) 
       {
          
    //$_xml .="\t<dato=\"" . $rows["dato"] . "\">\r\n";$_xml .="\t</dato>\r\n"; 
          
    $_xml .="\t<num_paquete>=\"" $rows["Paqnr"] . "\">\r\n";$_xml .="\t</num_paquete>\r\n"
          
    $_xml .="\t<empresa>=\"" $rows["Empresa"] . "\">\r\n";$_xml .="\t</empresa>\r\n"
          
    $_xml .="\t<codigo>=\"" $rows["Codigo"] . "\">\r\n";$_xml .="\t</codigo>\r\n"
          
    $_xml .="\t<envio>=\"" $rows["Envio"] . "\">\r\n";$_xml .="\t</envio>\r\n"
          
    $_xml .="\t<desde>=\"" $rows["Desde"] . "\">\r\n";$_xml .="\t</desde>\r\n"
          
    $_xml .="\t<peso_total>=\"" $rows["Peso_Total"] . "\">\r\n";$_xml .="\t</peso_total>\r\n"
          
    $_xml .="\t<peso>=\"" $rows["Peso"] . "\">\r\n";$_xml .="\t</peso>\r\n"
          
    $_xml .="\t<valor>=\"" $rows["Valor"] . "\">\r\n";$_xml .="\t</valor>\r\n"
          
    $_xml .="\t<codigo_env>=\"" $rows["Codigo_Env"] . "\">\r\n";$_xml .="\t</codigo_env>\r\n"
          
    $_xml .="\t<informacion>=\"" $rows["Information"] . "\">\r\n";$_xml .="\t</informacion>\r\n"
          
          }
          }
          else 
             { 
             
    $_xml .="\t<Query_Empty>=\"No Resulta\">\r\n"
             
    $_xml .="\t</Query_Empty>\r\n"
             } 
             
    //}
             
    $_xml .="</Caudat_xml>"
             
    fwrite($file$_xml); 
             
    fclose($file); 
             echo 
    "XML formato list.  <a href=\"resulta.xml\">Por Aqui el formato XML.</a>"?>
  14. #8
  15. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,939
    Rep Power
    902
    indenting INDEED is my friend

    but editing in this textbox i have yet to find a way to do it (aside from hitting spacebar) and figured it was a small enough example it was not necessary to take it to an editor and indent and bring it back.

    i also don't like the } else {

    i think brackets are deserving of their own line
  16. #9
  17. No Profile Picture
    Dissident
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2003
    Location
    New York
    Posts
    1,671
    Rep Power
    53
    Originally posted by angrypanda
    indenting INDEED is my friend

    but editing in this textbox i have yet to find a way to do it (aside from hitting spacebar) and figured it was a small enough example it was not necessary to take it to an editor and indent and bring it back.

    i also don't like the } else {

    i think brackets are deserving of their own line
    I hate this textbox thingy and do 90% of my code samples in textpad, no matter how small. hehe.

    I agree fully about } else {, but to each his own.
    Draelon


    PHP Manual :: MySQL Manual :: How to Ask Questions the Smart Way
    =======================================================

IMN logo majestic logo threadwatch logo seochat tools logo