Click to See Complete Forum and Search --> : Reading MySQL to display in HTM page problem


Nate Hunter
March 21st, 2008, 03:37 PM
I am a newby so parden my ignorence.

I am trying to display data from my db into a webpage. I can connect to the db but when I am writing to the webpage I am having problems in the echo. Any help would be appreciated. Here is my code:


<body>
<center>
<?php

// hostname or ip of server (for local testing, localhost should work)
$dbServer='myServer';

// username and password to log onto db server
$dbUser='myUser';
$dbPass='myPass';

// name of database
$dbName='myDbName';

$link = mysql_connect("$dbServer", "$dbUser", "$dbPass") or die("Could not connect");

mysql_select_db("$dbName", $link);
?>

<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Position</th>
<th>TimeSlot</th>
</tr>
<?php
$sql = "SELECT FirstName, LastName, Email, Phone, Position, TimeSlot FROM Volunteers";
$result = mysql_query ($sql);


while($row = mysql_fetch_array($query)) {
echo "<tr>"; //This is the part where I think I have the problem I start displaying text in my webpage of the rest of this code.
echo "<td>".$row['FirstName']."</td>";
echo "<td>".$row['LastName']."</td>";
echo "<td>".$row['Position']."</td>";
echo "<td>".$row['TimeSlot']."</td>";
echo "</tr>";
}

mysql_close($link);
?>
</table>
</center>
</body>

PeejAvery
March 21st, 2008, 03:47 PM
Can you be more specific? What problem are you having with the echoing of data? Also see my 3rd suggestion.

A couple of suggestions...

1. When returning so many columns of a table within a database, just use * instead of naming every column.

$sql = "SELECT * FROM Volunteers";

2. If you are just going to use the associative array, use mysql_fetch_assoc().

while($row = mysql_fetch_assoc($query)) {

3. When outputting HTML, do your best to use HTML only, and then echo PHP withing the HTML.

<?php
while($row = mysql_fetch_assoc($query)) {
?>
<tr>
<td><?php echo $row['FirstName']; ?></td>
<td><?php echo $row['LastName']; ?></td>
<td><?php echo $row['Position']; ?></td>
<td><?php echo $row['TimeSlot']; ?></td>
</tr>
<?php
}
?>

Nate Hunter
March 24th, 2008, 09:00 AM
ok I used your suggestions and I know longer get the problems I had before. Everything seems to work with the exception I have no data. How can I check to see if my query is reading correctly?

Here is all my code again just in case I missed something. Sorry for asking the questions I am a newby trying to learn on my own. Thanks for helping.



<?php

// hostname or ip of server (for local testing, localhost should work)
$dbServer='Severname';

// username and password to log onto db server
$dbUser='UserName';
$dbPass='Pass';

// name of database
$dbName='DBName';

$link = mysql_connect("$dbServer", "$dbUser", "$dbPass") or die("Could not connect");
mysql_select_db("$dbName", $link);
?>
<?php
$sql = "SELECT * FROM Volunteers";
$result = mysql_query ($sql);
while($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['FirstName']; ?></td>
<td><?php echo $row['LastName']; ?></td>
<td><?php echo $row['Position']; ?></td>
<td><?php echo $row['TimeSlot']; ?></td>
</tr>
<?php
}
mysql_close($link);
?>
</table>

PeejAvery
March 24th, 2008, 09:42 AM
Use the echo function to echo your actual query string. Then copy and paste that into phpMyAdmin. That will help you to see where it is failing.