I need help outputting the results in my DB

Codes here !

Moderators: macek, egami, gesf

User avatar
amp
New php-forum User
New php-forum User
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland
Contact:

I need help outputting the results in my DB

Postby amp » Tue Apr 01, 2003 4:27 am

Hi
I am fairly new to PHP & MySQL so please bear with me.

I have a php form that takes user input and sends it to the database, this works fine but I am having problems outputting the results from the database.
I am using a file called results.php to output the data but it's not working properly, it wont display any of my data. Here is my results.php file:

Code: Select all

<?php

include("config.php");
$dbh = mysql_connect ($dbhost, $dbuname, $dbpass) or die ( 'I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname) or die (mysql_error());

// perform query
$query = "SELECT *  FROM travel";
$result = mysql_query($query) or die("Error:" .mysql_error());

$prn = $row[0];
$date = $row[1];
$name = $row[2];
$dir = $row[3];
$purpose = $row[4];
$sponsor = $row[5];
$refund = $row[6];
$what_refundable = $row[7];
$flight_cost = $row[8];
$subsis_cost = $row[9];
$no_nights = $row[10];
$depart_location = $row[11];
$depart_time = $row[12];
$out_flight = $row[13];
$via_location = $row[14];
$via_arrive = $row[15];
$via_depart = $row[16];
$arrive_location = $row[17];
$arrive_time = $row[18];
$arrive_flight = $row[19];
$return_location = $row[20];
$return_time = $row[21];
$return_flight = $row[22];
$via_location2 = $row[23];
$via_arrive2 = $row[24];
$via_depart2 = $row[25];
$depart_time2 = $row[26];
$arrive_time2 = $row[27];
$flight_num2 = $row[28];


// retrive values
while ($row = mysql_fetch_array($result)) {
$echo = $row[0];
$echo = $row[1];
$echo = $row[2];
$echo = $row[3];
$echo = $row[4];
$echo = $row[5];
$echo = $row[6];
$echo = $row[7];
$echo = $row[8];
$echo = $row[9];
$echo = $row[10];
$echo = $row[11];
$echo = $row[12];
$echo = $row[13];
$echo = $row[14];
$echo = $row[15];
$echo = $row[16];
$echo = $row[17];
$echo = $row[18];
$echo = $row[19];
$echo = $row[20];
$echo = $row[21];
$echo = $row[22];
$echo = $row[23];
$echo = $row[24];
$echo = $row[25];
$echo = $row[26];
$echo = $row[27];
#$echo(".$prn");
}
// FORMAT RESULTS
//putting results into a array then looping thru and echoing the array
#$returned=mysql_num_rows($result);
#if($returned>1)
#{
#   while($row=mysql_fetch_array($result));
#         {
#                 echo $row["2"];
#                             echo("<table  border=\"0\" #celpadding=\"1\" cellspacing=\"1\">");

#$word_array = explode(",", $row['keywords']);
#$sql_select = "SELECT * FROM travel";
#$result = mysql_query($sql_select);
#while($query_data = mysql_fetch_row($result))

#echo "$prn prn<br />";
#echo $date.'date<br />';
#echo $sponsor.'sponsor<br />';
#echo $dir.'dir<br />';
#echo $refund.'refund<br />';

?>


The above gives me a blank page & I have tried it many different ways :cry:
What am I doing wrong here??

Thanks in advance for any replies :wink:

User avatar
amp
New php-forum User
New php-forum User
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland
Contact:

Postby amp » Tue Apr 01, 2003 6:48 am

OK, I've kinda sorted my problem but now my results.php is showing all the results twice. Here is my updated results.php

Code: Select all

<?php
include("config.php");
$dbh = mysql_connect ($dbhost, $dbuname, $dbpass) or die ( 'I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname) or die (mysql_error());

// perform query
$query = "SELECT *  FROM travel";
$result = mysql_query($query) or die("Error:" .mysql_error());

// retrive values
while ($row = mysql_fetch_array($result)) {
echo 'ID : ';
echo  $row[0];
echo '<br>';
echo 'Provisional Reservation Number: ';
echo  $row[1];
echo '<br>Date : ';
echo  $row[2];
echo '<br>Name :';
echo  $row[3];
echo '<br>Directorate:';
echo  $row[4];
echo '<br>Purpose:';
echo  $row[5];
echo '<br>Sponsor: ';
echo  $row[6];
echo '<br>';
echo  $row[7];
echo '<br>';
echo  $row[8];
echo '<br>';
echo  $row[9];
echo '<br>';
echo  $row[10];
echo '<br>';
echo  $row[11];
echo '<br>';
echo  $row[12];
echo '<br>';
echo  $row[13];
echo '<br>';
echo  $row[14];
echo '<br>';
echo  $row[15];
echo '<br>';
echo  $row[16];
echo '<br>';
echo  $row[17];
echo '<br>';
echo  $row[18];
echo '<br>';
echo  $row[19];
echo '<br>';
echo  $row[20];
echo '<br>';
echo  $row[21];
echo '<br>';
echo  $row[22];
echo '<br>';
echo  $row[23];
echo '<br>';
echo  $row[24];
echo '<br>';
echo  $row[25];
echo '<br>';
echo  $row[26];
echo '<br>';
echo  $row[27];
}

?>

User avatar
mammal
New php-forum User
New php-forum User
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

mysql_fetch_array

Postby mammal » Wed Apr 02, 2003 1:50 am

Hi there, you're problem may lie in the php command mysql_fetch_array. If you use this command the array key ($array_name[key]) must be the name of the column in the SQL database. If you use mysql_fetch_row instead you use the number of the column taken from the database. Therefore unless your columns are numbered this script will not work correctly. Try changing it to mysql_fetch_row and see if it works.

User avatar
amp
New php-forum User
New php-forum User
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland
Contact:

Postby amp » Wed Apr 02, 2003 3:12 am

Hi mammal
Thank you very much for your reply.
It turns out I had 2 identical entries in my DB so thats why I got the results twice :rolleyes:

If you use this command the array key ($array_name[key]) must be the name of the column in the SQL database.

I don't fully understand you, by ($array_name[key]) I assume you mean ($row[1]) ?? The columns in my DB are not numbered, they all have names so why is my script able to pull the data from the DB using $row[1] for example when my DB columns arent numbered?

My *very limited* understanding is that $row[1] is the second form variable from my form which is $_POST['prn'] & $row[2] would be the 3rd variable from my form, ie $_POST['name'] & so on & so on.
Am I making any sense here?
If you can clarify any of the above for me I would really appreciate it.
Thanks[/quote]

User avatar
mammal
New php-forum User
New php-forum User
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Trying to solve an easy problem with a complicated answer :)

Postby mammal » Wed Apr 02, 2003 3:24 am

Hiyah, just realised mysql_fetch_array also works by using numbers too. For instance if you can use:

$row[1] or $row[prn] etc...

I didnt want to mention duplicate entries as I would have thought you would have checked this. Thats the strange thing about PHP, the better you get at it the more likely to ignore the obvious :)

Glad I made some sense...

User avatar
amp
New php-forum User
New php-forum User
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland
Contact:

Postby amp » Wed Apr 02, 2003 3:41 am

Hiyah, just realised mysql_fetch_array also works by using numbers too. For instance if you can use:

$row[1] or $row[prn] etc...


Ah, thanks for clearing that up for me :wink:


Return to “mySQL & php coding”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

cron