Page 1 of 1

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

Posted: Wed Oct 27, 2004 4:39 am
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>

Posted: Wed Oct 27, 2004 4:53 am
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

Posted: Wed Oct 27, 2004 6:35 am
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

problem in your code

Posted: Wed Oct 27, 2004 6:37 am
by murthy
you have to use like this,

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

:)