WJB
WJB

Reputation: 9

Need to display a MySQL table on a page using PHP

I'm trying to get a table to display and keep getting an error. Could someone out there with better coding skills help me?

Here is the code:

<?php 

// connect to the database

$host = "###";
$username = '###';
$pass = '###';

mysql_connect($host,$username,$pass) or die(mysql_error());
mysql_select_db("Employees") or die(mysql_error());

// select everything from the table

$query = "SELECT * FROM Employees";
$result = mysql_query($query') or die(mysql_error());

echo "<table>";
echo "<tr>";

while( ($row = mysql_fetch_array($result)))
{
    echo "<td>".$row['employeeid']."</td>";
    echo "<td>".$row['firstname']."</td>";
    echo "<td>".$row['lastname']."</td>";
    echo "<td>".$row['department']."</td>";
}

echo "</tr>";
echo "</table>";

// disconnect from the database

mysql_close();

?>


When the page runs it yields the following error:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/35/4683335/html/crosshill/display.php on line 36

Line 34 is: echo "<td>".$row['employeeid']."</td>";

Upvotes: 0

Views: 256

Answers (2)

Sir
Sir

Reputation: 8280

I noticed your database is called Employees as well as your table, is this a mistake or are they both the same name?

You also have syntax error here:

$query = "SELECT * FROM Employees";
$result = mysql_query($query') or die(mysql_error());

Remove the ' so it is like this:

$result = mysql_query($query) or die(mysql_error());

Try this:

$query = "SELECT * FROM Employees";
$result = mysql_query($query) or die(mysql_error());

if(mysql_num_rows($result)< 1){

   echo 'no rows founds';

} else {


echo "<table>";
echo "<tr>";

while( ($row = mysql_fetch_assoc($result)))
{
    echo "<td>".$row['employeeid']."</td>";
    echo "<td>".$row['firstname']."</td>";
    echo "<td>".$row['lastname']."</td>";
    echo "<td>".$row['department']."</td>";
}

echo "</tr>";
echo "</table>";

}

Side note: As suggested by others you should switch to either mysqli_* or perhaps pdo.

Upvotes: 0

Ignacio Ocampo
Ignacio Ocampo

Reputation: 2713

I suggest that you need check 3 points:

You could use or die(mysql_error()) in dev environment to check if you have an error (with sql functions related).

  1. Connection

    mysql_connect($host,$username,$pass) or die(mysql_error());

  2. Select db

    mysql_select_db("Employees") or die(mysql_error());

  3. Query (I see that you query are correct, but probably you table name are wrong, remember that names are case sensitive)

    $result = mysql_query($query) or die(mysql_error());

Upvotes: 1

Related Questions