SQL queries results in table???

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
peoplespaul
Forum Newbie
Posts: 3
Joined: Wed Apr 05, 2006 3:08 pm

SQL queries results in table???

Post by peoplespaul »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hey,

The first question is that i have is how to make one large form which retrieves data from a database and outputs it into a table.

the current code i have is outputting it to a table but not correctly:? eg if there are four results to the query only 3 will be outputted :S and the 1st line in therewill be each row's title's, it is most likey something simple. would be grateful for the help.


below is the code for the form in search.php

Code: Select all

<FORM METHOD= "post" ACTION= "dest_loc.php">
            
          <H4>
             Departure location
          
       
            <!-- text box with attributes set -->
              <INPUT NAME ="dep_loc" TYPE="text" SIZE=40 MAXLENGTH=40>
         </H4>
         <H4>
              Destination location
          
            <INPUT NAME ="arr_loc" TYPE="text" SIZE=20 MAXLENGTH=40>
         
       
         <input type="submit" value="Search"></H4>
        
      
         
</FORM>

here is the php in dept_loc that i have done

Code: Select all

<?php

$server=mysql_connect("clun.scit.wlv.ac.uk","demo","");
if (!$server):
	print ("error connecting to server");
	exit;
	
endif;
mysql_select_db("cp1082",$server);

$dep_loc = $HTTP_POST_VARS['dep_loc'];
$arr_loc = $HTTP_POST_VARS['arr_loc'];
$sql="SELECT * FROM `airline`";

if($dep_loc!="")
  $sql = $sql." WHERE ffrom = '$dep_loc'";

if($arr_loc!="")
  $sql = $sql." AND fto = '$arr_loc'";


echo $sql."<br><br>";
$result=mysql_query($sql);

if (mysql_error()):
	print ("there has been an error<BR>".mysql_error());
	exit;
endif;
print "There have been ".mysql_num_rows($result)." records returned<BR>";

while ($row=mysql_fetch_array($result))
{

   
   echo '<table border="1">
       <TR><TD>ffrom</TD>
       <TD>fto</TD>
       <TD>flight_no</TD>
       <TD>departure</TD>
       <TD>arrival</TD></TR>';

while (list ($ffrom, $fto, $flight_no, $departure, $arrival) = mysql_fetch_row($result)) {
       echo "<TR><TD>$ffrom</TD>
       <TD>$fto</TD>
       <TD>$flight_no</TD>
       <TD>$departure</TD>
       <TD>$arrival</TD</TR>";
}
echo '</table>';


mysql_close($server);
}



?>





the second question i have is my php doesnt seem to validate any idea's??

thanks for your help

Paul


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code is written such that the first record found is thrown away due to calling mysql_fetch_array() prior to calling mysql_fetch_row() in your nested while loops.
peoplespaul
Forum Newbie
Posts: 3
Joined: Wed Apr 05, 2006 3:08 pm

How do i fix this then?

Post by peoplespaul »

how do i fix this problem with that reply?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would suggest losing the outer while loop as you aren't actually using it other than starting and stopping a single table, which is not exactly the point of a loop control.
peoplespaul
Forum Newbie
Posts: 3
Joined: Wed Apr 05, 2006 3:08 pm

still don't understand sorry

Post by peoplespaul »

i'm fairly new to php.... remove the outer while loop? can u go into a little more detail thank you for your help

Paul 8)
Post Reply