Home > Archive > PHP Language > August 2005 > looping query
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Konrad Tuszkowski 2005-07-30, 4:59 pm |
| hello
how to make a loop to query db 5 times. i need some general sketch.
thx
konrad
| |
| J.O. Aho 2005-07-30, 4:59 pm |
| Konrad Tuszkowski wrote:
> hello
> how to make a loop to query db 5 times. i need some general sketch.
for($i=0;$i<5;$i++) {
$reulst=mysql_query("Select * from Table LIMIT 1");
}
a loop that query a database 5 times, no data displayed. Could be a bit more
useful to be more specific what you want to do.
More info about mysql_query can be found at:
http://www.php.net/manual/en/function.mysql-query.php
//Aho
| |
|
|
| Konrad Tuszkowski 2005-07-31, 3:59 am |
| thank u.
i just needed some direction, not a direct solution. no fun then :):):)
but thanks :):):):)
if ur interested i am struggling with some issues in my scrtipt (see "form
problem" post by me)
"J.O. Aho" <user@example.net> wrote in message
news:3l29nbF10ljs0U1@individual.net...
> Konrad Tuszkowski wrote:
>
> for($i=0;$i<5;$i++) {
> $reulst=mysql_query("Select * from Table LIMIT 1");
> }
>
> a loop that query a database 5 times, no data displayed. Could be a bit
more
> useful to be more specific what you want to do.
>
> More info about mysql_query can be found at:
> http://www.php.net/manual/en/function.mysql-query.php
>
>
>
> //Aho
| |
| J.O. Aho 2005-07-31, 3:59 am |
| Konrad Tuszkowski wrote:
> thank u.
> i just needed some direction, not a direct solution. no fun then :):):)
> but thanks :):):):)
> if ur interested i am struggling with some issues in my scrtipt (see "form
> problem" post by me)
/* Create an array and insert values that has been set */
if(isset($A) && !empty($A)) {
/* As $A is the first possible value, the create the array*/
$looparray=$array($A);
}
if(isset($B) && !empty($B)) {
/* There is an $B value */
if(isset($looparray)) {
/* If the array has already been set, insert this value*/
array_push($looparray,$B);
} else {
/* if the array hasn't been created, make it now */
$looparray=$array($B);
}
}
if(isset($C) && !empty($C)) {
if(isset($looparray)) {
array_push($looparray,$C);
} else {
$looparray=$array($C);
}
}
if(isset($D) && !empty($D)) {
if(isset($looparray)) {
array_push($looparray,$D);
} else {
$looparray=$array($D);
}
}
if(isset($E) && !empty($E)) {
if(isset($looparray)) {
array_push($looparray,$E);
} else {
$looparray=$array($E);
}
}
/* We echo how many variables has been set */
echo"<br>".count($looparray)."<br>\n";
/* We check that there has been values set, or else we won't try to fetch
anything from the database */
if(isset($looparray)) {
/* We loop through the array and fetch each value from database*/
for($i=0;$i<count($looparray);$i++) {
$query ="SELECT matom,nazwa FROM chem WHERE symb LIKE
'".$looparray[$i]."' LIMIT 1";
$result = mysql_query ($query) or die ("Zapytanie zakonczone
niepowodzeniem");
$wiersz = mysql_fetch_array($result);
$a=$wiersz["matom"];
$b=$wiersz["nazwa"];
$tab_b[]=$b;
$tab_a[]=$a;
echo "Masa molowa <b>$b"."u</b> wynosi $a<br>\n";
}
} else {
/* No values set, we need to give an error message */
echo "brak danych<br>\n";
}
This would give you all five, if all five values are set, if less values are
set, then just those times are a value from the database fetched. The code is
untested and I have just got up from bed, so it may not work directly but may
need a bit tweaking, but I think you get a pretty good idea from it what to do.
//Aho
| |
| Konrad Tuszkowski 2005-07-31, 3:59 am |
| thx, and i did it like this:
if ($_POST['A']) { $tablica[0]=$_POST['A']; }
if ($_POST['u']) { $wartosci[0]=$_POST['u']; }
if ($_POST['B']) { $tablica[1]=$_POST['B']; }
if ($_POST['w']) { $wartosci[1]=$_POST['w']; }
if ($_POST['C']) { $tablica[2]=$_POST['C']; }
if ($_POST['x']) { $wartosci[2]=$_POST['x']; }
if ($_POST['D']) { $tablica[3]=$_POST['D']; }
if ($_POST['y']) { $wartosci[3]=$_POST['y']; }
if ($_POST['E']) { $tablica[4]=$_POST['E']; }
if ($_POST['z']) { $wartosci[4]=$_POST['z']; }
$wiersze=count($tablica);
for($i=0;$i<$wiersze;$i++) {
$result=mysql_query("SELECT nazwa,matom FROM chem WHERE symb =
'$tablica[$i]' ");
$wiersz = mysql_fetch_array($result);
echo "Masa molowa <b>".$wiersz['nazwa']."u</b> wynosi
".$wiersz['matom']."<br>";
}
it works as i want for now,
because i am a totally newbe, so i prefer simple 9but sometimes not smart
enouh) solutions. will try ur if mine stucks somewhere.
thx again
konrad
| |
| Witold 2005-08-09, 5:01 pm |
|
Użytkownik "Konrad Tuszkowski" <konrad.t@op.pl> napisał w wiadomości
news:dcgq1b$fo7$2@news.onet.pl...
> hello
> how to make a loop to query db 5 times. i need some general sketch.
>
> thx
>
> konrad
>
Is there any reasons that You ask on alt.comp.lang.php, alt.php? hmmm....
don/t know (like) Polish?
Grzesiek
|
|
|
|
|