thwikehu1990 Posted January 19, 2015 Share Posted January 19, 2015 (edited) 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 January 19, 2015 by thwikehu1990 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 19, 2015 Share Posted January 19, 2015 What is the error message? That would help us. Quote Link to comment Share on other sites More sharing options...
thwikehu1990 Posted January 19, 2015 Author Share Posted January 19, 2015 My error is just my while echo appearing in and out of my tables on the page. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 19, 2015 Share Posted January 19, 2015 Whatever does that mean? Quote Link to comment Share on other sites More sharing options...
thwikehu1990 Posted January 19, 2015 Author Share Posted January 19, 2015 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 19, 2015 Share Posted January 19, 2015 (edited) 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 January 19, 2015 by ginerjm Quote Link to comment Share on other sites More sharing options...
Barand Posted January 19, 2015 Share Posted January 19, 2015 while($employees=mysql_fetch_assoc($records)) { $data .= "<td>".$Employee['ID']."</td>"; Those should match. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 19, 2015 Share Posted January 19, 2015 Already covered Quote Link to comment Share on other sites More sharing options...
Barand Posted January 19, 2015 Share Posted January 19, 2015 I grovel on my knees, Sir. Please accept my most humble apologies for missing your comments and just reading your code. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 19, 2015 Share Posted January 19, 2015 Actually not my code but the OP's which I left there- commented - for his benefit Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 19, 2015 Share Posted January 19, 2015 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 - 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.