Page 1 of 1
[SOLVED]-Getting multipul rows from database.
Posted: Thu Jan 29, 2004 11:08 am
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 {
Posted: Thu Jan 29, 2004 11:16 am
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?

Posted: Thu Jan 29, 2004 11:32 am
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
<?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>
<peso_total>="9807435"></peso_total>
<peso>="98769876"></peso>
<valor>="876976"></valor>
<codigo_env>=""></codigo_env>
<informacion>="comnt"></informacion>
</Caudat_xml>
its should be like this
Code: Select all
<?xml version="1.0" encoding="UTF-8" ?>
- <Caudat_xml>
// first $row
<num_paquete>="777000"></num_paquete>
<empresa>="mycom"></empresa>
<codigo>="899"></codigo>
<envio>="to"></envio>
<desde>="from"></desde>
<peso_total>="9807435"></peso_total>
<peso>="98769876"></peso>
<valor>="876976"></valor>
<codigo_env>=""></codigo_env>
<informacion>="comnt"></informacion>
// second $row
<num_paquete>="22222"></num_paquete>
<empresa>="mycom2"></empresa>
<codigo>="999"></codigo>
<envio>="tomambo"></envio>
<desde>="frommambo"></desde>
<peso_total>="90000"></peso_total>
<peso>="90000"></peso>
<valor>="$500"></valor>
<codigo_env>=""></codigo_env>
<informacion>="comnt"></informacion>
ect..... with the other 38 $rows
</Caudat_xml>
Posted: Thu Jan 29, 2004 5:57 pm
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.
Posted: Thu Jan 29, 2004 10:59 pm
by timvw
have a look at xpath
Posted: Fri Jan 30, 2004 8:53 am
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
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");
Posted: Fri Jan 30, 2004 3:10 pm
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