[SOLVED]-Getting multipul rows from database.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

[SOLVED]-Getting multipul rows from database.

Post by ol4pr0 »

Oke i got this far now.. i got it to write a xml document however i can i loop true all the tables in my db.

Code: Select all

$result = mysql_query($query);
$num = mysql_num_rows($result);

if ($num != 0) {
 $file= fopen("resulta.xml", "w");
 $_xml ="<?xml version="1.0" encoding="UTF-8" ?>\r\n";
 $_xml .="<Caudat_xml>\r\n";
 while ($rows = mysql_fetch_array($result)) {
 if ($rows["Paqnr"]) {
 $_xml .="\t<num_paquete>="" . $rows["Paqnr"] . "">\r\n";
 $_xml .="\t</num_paquete>\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"> XML.</a>";
 } else {
 echo "No Records found por $hoy";	
 }
i have tried to repeat the following but and by changing the xml tags but that didnt go well ofcourse :?

Code: Select all

while ($rows = mysql_fetch_array($result)) {
 if ($rows["Empresa"]) {
 $_xml .="\t<Empresa>="" . $rows["Empresa"] . "">\r\n";
 $_xml .="\t</Empresa>\r\n"; } else {
Last edited by ol4pr0 on Fri Jan 30, 2004 3:11 pm, edited 3 times in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

ok, i'm not sure what you are trying to do, so i'm gonna take a stab in the dark..

are you just wanting to store multiple values from your table into a variable that you can call later? that variable being $_xml ?

if so, why not just do something like this...

Code: Select all

$myarray = array();
while($rows = mysql_fetch_array($result))
{
   if($rows['Empresa'])
   {
      $myarray[] = "\t<Empresa>="" . $rows['Paqnr'] . "">\r\n";
      $myarray[] = "\t</Empresa>\r\n";
    }
    else
    {
       // something...
    }
}
dunno, i'm not sure what you want to do. posting the code is fine, but could you be a bit more specific? :P
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Oke well i am trying to get all the info from my table pongo and have it written to a xml file

now i got it to get one row but not all the rows like it should ( DISC LIMIT 40)

Also i do not following what youre trying to tell me with

Code: Select all

$myarray = array(); 
while($rows = mysql_fetch_array($result)) 
{ 
   if($rows['Empresa']) 
   { 
      $myarray[] = "\t<Empresa>="" . $rows['Empresa'] . "">\r\n"; 
      $myarray[] = "\t</Empresa>\r\n"; 
    } 
    else 
    { 
       // something... 
    } 
}
This will surely mess up my well formatted XML document. i gave it some thought and even a try. and ass i expected it did mess up my xml formae

Any other ideas on how to get all the 40 rows ?

Or else by $date say like only today?!



let me show the whole script as i got it now.

Code: Select all

<?
$hoy = date("F d Y");
$dbase ="home";
$tablename ="pongo";
$connect= mysql_connect('localhost')
	or die("Cant connect: " .mysql_error());

// connectarse con el base datos


mysql_select_db($dbase, $connect); 

// consultar el tabla pongo ( cambiar cuando listo )
// Only execute this code if the Paqnr variable isset 
   
$query = "SELECT Empresa, Paqnr, Codigo, Envio, Desde, Information, Comentario, Peso_Total, Valor, Codigo_Env, Peso FROM pongo ORDER BY `Empresa` DESC LIMIT 40"; 

//  $result = mysql_query($query);
//  if($row = mysql_fetch_array($result))
//  {


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

if ($num != 0)
	{
	$file= fopen("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<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";
		//$_xml .="\t<dato="" .$hoy.  "">\r\n";$_xml .="\t</dato>\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>";
?>
as you can see below it only displayed one row, as it should retrieve like 40 rows ;-)

Code: Select all

&lt;?xml version="1.0" encoding="UTF-8" ?&gt; 
- &lt;Caudat_xml&gt;
  &lt;num_paquete&gt;="777000"&gt;&lt;/num_paquete&gt; 
  &lt;empresa&gt;="mycom"&gt;&lt;/empresa&gt; 
  &lt;codigo&gt;="899"&gt;&lt;/codigo&gt; 
  &lt;envio&gt;="to"&gt;&lt;/envio&gt; 
  &lt;desde&gt;="from"&gt;&lt;/desde&gt; 
  &lt;peso_total&gt;="9807435"&gt;&lt;/peso_total&gt; 
  &lt;peso&gt;="98769876"&gt;&lt;/peso&gt; 
  &lt;valor&gt;="876976"&gt;&lt;/valor&gt; 
  &lt;codigo_env&gt;=""&gt;&lt;/codigo_env&gt; 
  &lt;informacion&gt;="comnt"&gt;&lt;/informacion&gt; 
  &lt;/Caudat_xml&gt;
its should be like this

Code: Select all

&lt;?xml version="1.0" encoding="UTF-8" ?&gt; 
- &lt;Caudat_xml&gt;
 // first $row
  &lt;num_paquete&gt;="777000"&gt;&lt;/num_paquete&gt; 
  &lt;empresa&gt;="mycom"&gt;&lt;/empresa&gt; 
  &lt;codigo&gt;="899"&gt;&lt;/codigo&gt; 
  &lt;envio&gt;="to"&gt;&lt;/envio&gt; 
  &lt;desde&gt;="from"&gt;&lt;/desde&gt; 
  &lt;peso_total&gt;="9807435"&gt;&lt;/peso_total&gt; 
  &lt;peso&gt;="98769876"&gt;&lt;/peso&gt; 
  &lt;valor&gt;="876976"&gt;&lt;/valor&gt; 
  &lt;codigo_env&gt;=""&gt;&lt;/codigo_env&gt; 
  &lt;informacion&gt;="comnt"&gt;&lt;/informacion&gt; 
  // second $row
  &lt;num_paquete&gt;="22222"&gt;&lt;/num_paquete&gt; 
  &lt;empresa&gt;="mycom2"&gt;&lt;/empresa&gt; 
  &lt;codigo&gt;="999"&gt;&lt;/codigo&gt; 
  &lt;envio&gt;="tomambo"&gt;&lt;/envio&gt; 
  &lt;desde&gt;="frommambo"&gt;&lt;/desde&gt; 
  &lt;peso_total&gt;="90000"&gt;&lt;/peso_total&gt; 
  &lt;peso&gt;="90000"&gt;&lt;/peso&gt; 
  &lt;valor&gt;="$500"&gt;&lt;/valor&gt; 
  &lt;codigo_env&gt;=""&gt;&lt;/codigo_env&gt; 
  &lt;informacion&gt;="comnt"&gt;&lt;/informacion&gt; 

ect..... with the other 38 $rows 
  &lt;/Caudat_xml&gt;
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

oh my bad man, i guess my post was more stupid than i thought. i've never used xml with php, and hadn't relized that $_XML was already defined for xml heh..

n/m. sorry i think someone with more expertise could help you though.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

have a look at xpath
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Its c00l infolock.

xpath? isnt that making use of $dom-> ?
my script doesnt use that since i am on a win2k laptop here. and pear install on a win2k machine is like a biatch :oops:

Its just that it wont display all rows.

It will give me a nice formatted xml document. i just cant figure out why it just shows me 1 $row instead of all $rows. thats what bugging me.

I have tried this one 2 and that didnt get me anywhere just that it changed displaying the second $row only instead of the 1st $row so i am kinda stuck on getting all $rows.

Code: Select all

$result = mysql_query($query) or die (mysql_error()); 
If ($rows = mysql_fetch_array($result))
		{
	$file= fopen("resulta.xml", "w");
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Thanks for all those who gave it a look, and those who gave it a hand ;-)

It all working and the way it should be is posted on

viewtopic.php?t=17473
Post Reply