Forum Moderators: coopster

Message Too Old, No Replies

retrieving data from sql into table

         

JuicyScript

8:19 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



I want my values to display in my table.But all am getting are errors.
I search thru the internet but am not really getting a neat way of doing it.I dont want to echo tag like "echo "</td><td>";

echo $row['stud_name'];
etc...
I just want to echo is back into my form.
<?php
include_once('config.php');
require_once('auth2.php');
include("session.html")

//echo $_SESSION['SESS_LOGIN'];
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}

$sql="SELECT * FROM members WHERE member_id = ".$_SESSION['SESS_MEMBER_ID'];
//echo print_r($sql);


$query=mysql_query($sql);
$executor=@mysql_fetch_array($query);
?>
Html tags here--------
<table width="634" align="center" class="FormInput">
<tr>
<td width="193">Mr/Mrs/Miss</td>
<td width="442"><? while($row=mysql_fetch_array($result)){ echo $row['Mr'];}?> </td>
</tr>
<tr>
<td width="193">Sur Name</td>
<td width="442"><? while($row=mysql_fetch_array($result)){ echo $row['surname'];}?></td>
</tr>
<tr>
<td width="193">Other Names</td>
<td width="442"><? while($row=mysql_fetch_array($result)){ echo $row['othernames'];}?></td>
</tr>
<tr>
<td width="193">Gender</td>
<td width="442"><? while($row=mysql_fetch_array($result)){ echo $row['gender'];}?></td>
</tr>
<tr>
<td width="193">Date of Birth</td>
<td width="442"><label>
<? while($row=mysql_fetch_array($result)){ echo $row['drpdate'];}?>
</label>
<label>
<? while($row=mysql_fetch_array($result)){ echo $row['drpmonth'];}?>
</label>
<label>
<? while($row=mysql_fetch_array($result)){ echo $row['drpYear'];}?>
</label> </td>
</tr>
<tr>
<td width="193" height="32">Place of Birth</td>
<td width="442" height="32"><? while($row=mysql_fetch_array($result)){ echo $row['pob'];}?></td>
</tr>
<tr>
<td width="193">Nationality</td>
<td width="442"><? while($row=mysql_fetch_array($result)){ echo $row['drpcounty'];}?></td>
</tr>
<tr>
<td width="193">Postal address<span class="style5">(which you can be reach quickly)</span></td>
<td width="442"><? while($row=mysql_fetch_array($result)){ echo $row['drpcounty'];}?></td>
</tr>
<tr>
<td width="193">Permanent Address</td>
<td width="442"><? while($row=mysql_fetch_array($result)){ echo $row['postaddress'];}?></td>
</tr>

Readie

8:29 pm on Feb 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this:

include("session.html")

needs to be this:

include("session.html");

JuicyScript

10:15 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



I corrected dat and got this error across where i echoed the value.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program_Files\wamp\www\Healthreg\thankyou.php on line 57

Readie

11:05 pm on Feb 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$query=mysql_query($sql);

change that to

$result=mysql_query($sql);

or change all the mysql_fetch_array's to

mysql_fetch_array($query);

rocknbil

2:05 am on Feb 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take that advice, then,

am not really getting a neat way of doing it.I dont want to echo tag like "echo "</td><td>";


What you're doing here - spurts of PHP inside a plain HTML table - is not all that neat either. :-) It's a common approach in PHP, but it's really hard to maintain and debug. We'll look at that next - but first, you have something else wrong, or at the very least, inefficient, too:

<td width="442"><? while($row=mysql_fetch_array($result)){ echo $row['Mr'];}?> </td>

A while loop is intended for iterating through multiple values. So first what you want here is an if, but consider, What if there's no result? Sure it will be blank, you could leave it blank, but for an end user, what does that add up to? A big question mark over their head, "what's going on?"

Here's something that addresses all three problems. I'll only output three rows of the table for clarity.


$executor=@mysql_fetch_array($result);
if ($row=mysql_fetch_array($result)) {
$out = '
<table width="634" align="center" class="FormInput">
<tr><td width="193">Mr/Mrs/Miss</td>
<td width="442">' . $row['Mr'] . '</td></tr>
<tr><td width="193">Sur Name</td>
<td width="442">' . $row['surname'] . '</td></tr>
<tr><td width="193">Other Names</td>
<td width="442">' . $row['othernames'] . '</td></tr>
</table>
'; // end of OUT
}
else { $out = "<p>There are no records to display.</p>"; }
echo $out;


The above is not perfect, the best way would be to have a plain HTML template with markers for the values, and substitute them out as you go. But it's an improvement, I guess.

JuicyScript

5:00 pm on Feb 18, 2010 (gmt 0)

10+ Year Member



What if you do want the table to display or give a message like"There are no records to display" if a field is empty.For example if surname is empty the table shd not display

rocknbil

5:46 pm on Feb 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$fields = Array(
'Mr/Mrs/Miss' => 'Mr',
'Sur Name' => 'surname',
'Other Names' => 'othernames'
);
if ($row=mysql_fetch_array($result)) {
$out = '
<table width="634" align="center" class="FormInput">
';
foreach ($fields as $key=>$value) {
if (isset($row[$value]) and ($row[$value] != '')) {
$out .= '<tr><td width="193">' . $key . ' </td><td width="442">' .
$row[$value] . '</td></tr>';
}
}
$out .= '
</table>
'; // end of OUT
}
else { $out = "<p>There are no records to display.</p>"; }
echo $out;

JuicyScript

10:44 am on Feb 19, 2010 (gmt 0)

10+ Year Member



Thanks a lot but what i want was
like this....if it's possible
$executor=@mysql_fetch_array($result);
if ($row=mysql_fetch_array($result)and($row('surname') != '')) { $out = ' <table width="634" align="center" class="FormInput"> <tr><td width="193">Mr/Mrs/Miss</td> <td width="442">' . $row['Mr'] . '</td></tr> <tr><td width="193">Sur Name</td> <td width="442">' . $row['surname'] . '</td></tr> <tr><td width="193">Other Names</td> <td width="442">' . $row['othernames'] . '</td></tr> </table> '; // end of OUT }