Fetching from an array into html tales - help!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrwaterfield
Forum Newbie
Posts: 3
Joined: Wed Oct 27, 2004 4:15 am

Fetching from an array into html tales - help!!!

Post by mrwaterfield »

I am trying to fetch info from my 'employee' mysql db into an html table using arrays. However, in the first column I get all the variables about the employee ie (ie employee id, name, job, dep id), in the next three variables ie (ie name, job, dep id) two in the next then only the required info in the dep id column. Can anyone tell me how to stop this...I've been at it for hours now without a breakthru!
Thanks
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Title here!</title>
</head>
<body>
<?php

$conn = mysql_connect("localhost","root","*****")or die(mysql_error());
echo $conn;

mysql_select_db("employee",$conn);

$sql = "SELECT * FROM employee" or die(mysql_error());
$result=mysql_query($sql,$conn)or die(mysql_error());
echo"<table><tr><th>Employee ID</th><th>Name</th>
<th>Job</th><th>Department ID</th></tr>";
while($empArray = mysql_fetch_array($result))
$emp_id = $empArray['employee_id'].
$name = $empArray['name'].
$job = $empArray['job'].
$dep_id = $empArray['department_id'];

echo "<tr><td>$emp_id</td><td>$name</td><td>$job</td><td>$dep_id</td></tr>";
echo;"</table>";
mysql_close($conn);
?>
</body>
</html>
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

Hi there

in your code

Code: Select all

$emp_id = $empArray['employee_id'].
$name = $empArray['name'].
$job = $empArray['job'].
$dep_id = $empArray['department_id'];
lines 1, 2 and 3 end with a fullstop - which in PHP is the string concatenation character. Replace these with the semi-colon (the statement terminator) and all should be well.

Also, note that you have an extra semi-colon on this line
echo;"</table>";
It should be

Code: Select all

echo "</table>";
Hope this helps
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Code: Select all

while($empArray = mysql_fetch_array($result))
should be

Code: Select all

while($empArray = mysql_fetch_assoc($result))
You may also want to look up the extract command to make variables based on the array indexes. (not required but useful).
http://www.php.net/manual/en/function.extract.php
Last edited by CoderGoblin on Wed Oct 27, 2004 6:39 am, edited 1 time in total.
murthy
Forum Commoner
Posts: 50
Joined: Fri Aug 20, 2004 1:22 am
Location: India
Contact:

problem in your code

Post by murthy »

you have to use like this,

while($empArray = mysql_fetch_array($result,MYSQL_ASSOC))

:)
Post Reply