Jump to content

Displaying Data from a Table


thwikehu1990

Recommended Posts

Hi all!

I've been recently messing with PHP and MySQL to attempt to create a search function for my tables (too advanced, so I toned it down to learning to just display data from my table).  Anyways, I'm watching a YouTube video on how to do it, and I'm copying his code exactly, but I'm still getting an error.

 

Can anyone help?  Thanks. 

 

Edit: Sorry, my error was snuggled down below the code.  Anyways, my error is just my while echo appearing in and out of my tables on the page.

 

My code:

<?php

//Make connection
mysql_connect('localhost', 'root', 'test');

// select db
mysql_select_db('testdatabase');

$sql="SELECT * FROM employees";

$records=mysql_query($sql);



?>



<html>
<head>
<title>Employee Data</title>
</head>

<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>

<th>ID</th>
<th>First name</th>
<th>Surname</th>
<th>Age</th>
</tr>

<?php

while($employees=mysql_fetch_assoc($records)){
	echo "<tr>";
	echo "<td>".$Employee['ID']."</td>";
	echo "<td>".$Employee['First name']."</td>";
	echo "<td>".$Employee['Surname']."</td>";
	echo "<td>".$Employee['Age']."</td>";
	echo "</tr>";
}
// end while
?>
</table>
</body>
</html>
Edited by thwikehu1990
Link to comment
Share on other sites

while($employees=mysql_fetch_assoc($records)){
	echo "<tr>";
	echo "<td>".$Employee['ID']."</td>";
	echo "<td>".$Employee['First name']."</td>";
	echo "<td>".$Employee['Surname']."</td>";
	echo "<td>".$Employee['Age']."</td>";
	echo "</tr>";
}

The contents of that while loop appear as text on my web page.

Link to comment
Share on other sites

Try this:

<?php
session_start();
// ALWAYS TURN ON ERROR CHECKING DURING DEVELOPMENT!!!
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
//***********************
//Make connection
mysql_connect('localhost', 'root', 'test'); 
// select db
mysql_select_db('testdatabase'); 
$sql = "SELECT * FROM employees"; 
$records = mysql_query($sql);
//  You should really check the result of the query call to be sure it ran
if (!$records)
{
	echo "Error in query - query is $sql";
	exit();
}
$data = '<table width="600" border="1" cellpadding="1" cellspacing="1">';
$data .= '<tr>'; 
$data .= '<th>ID</th><th>First name</th><th>Surname</th><th>Age</th>';
$data .= '</tr>'; 
//  PHP IS CASE-SENSITIVE Change this to $Employee!!!
//  ALSO - JUST NOTICED that you have an 's' here and not later.  Remove it from 'employees'
while($employees=mysql_fetch_assoc($records))
{	
	$data .= "<tr>";	
	$data .= "<td>".$Employee['ID']."</td>";	
	$data .= "<td>".$Employee['First name']."</td>";	
	$data .= "<td>".$Employee['Surname']."</td>";	
	$data .= "<td>".$Employee['Age']."</td>";	
	$data .= "</tr>";
}
echo "<html><head><title>Employee Data</title></head>";
echo "<body>";
echo $data;
echo "</body></html>";
exit();
Note my comments in your code. PS - you REALLY should NOT be using the MySQL_* functions. They are far out-dated and deprecated -meaning that PHP will drop them soon. Read the manual on PDO if your server supports it or use the mysqlI functions if not. Edited by ginerjm
Link to comment
Share on other sites

The contents of that while loop appear as text on my web page.

 

 

it would help if showed exactly what you got as output in your browser, but i'll take a guess it looked like this -

 

post-144491-0-19354300-1421710580_thumb.png

 

if so, either you don't have php installed (or it's not working) on your web server, or you used a file extension other than .php, or you opened the file directly in your browser or editor.

 

you need to browse to the url of the file, on a web server that has php installed. if you have a localhost development system installed, the url would be similar to - http://localhost/your_file.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.