0

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>";

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
WJB
  • 9
  • 1
  • 2
  • 5
  • 1
    possible duplicate of [Warning: mysql\_result(): supplied argument is not a valid MySQL result resource in (...) on line 4](http://stackoverflow.com/questions/13675232/warning-mysql-result-supplied-argument-is-not-a-valid-mysql-result-resource) – Quentin Dec 14 '13 at 23:29
  • 3
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Dec 14 '13 at 23:29
  • I don't think my hosting provider offers anything else. I'm just trying to do a simple assignment with the resources I have at my disposal. But I appreciate you point this out and when I change providers soon will use that feedback in selecting a new one. – WJB Dec 14 '13 at 23:46
  • If your provider uses a version of PHP that doesn't come with PDO, then run away, run far far away. They are using a version of PHP which is no longer supported. – Quentin Dec 14 '13 at 23:49

2 Answers2

1

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());

Ignacio Ocampo
  • 2,693
  • 1
  • 20
  • 31
  • Thanks very much! I've added those 3 check points and it passes all of them. I now get a different error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in ####/display.php on line 36 – WJB Dec 14 '13 at 23:43
  • Line 33 is "echo "".$row['employeeid']."";" ( I changed the order to match the table, thinking that might be the problem. It did not help – WJB Dec 14 '13 at 23:44
  • the only thing removed is all the database info. Line 36 is the first attempt to print after the fetch array command. I'll update the code in the original question to reflect my edits. Thanks for the continued help! – WJB Dec 14 '13 at 23:50
  • Just updated the original question to reflect changes to code. Thanks! – WJB Dec 14 '13 at 23:54
  • 1
    Now, you have a quote error in `$result = mysql_query($query') or die(mysql_error());` you sould have `$result = mysql_query($query) or die(mysql_error());` – Ignacio Ocampo Dec 14 '13 at 23:55
  • You guys are all very great helps and good coders. I removed that and it went on to other errors. :) See above for other solutions. It is now working, but all item from that fetch_array request are on the same line. Any way to easily remedy that? And should I keep editing the question or start new ones in cases like this? – WJB Dec 15 '13 at 03:29
  • I think you should open a new question and paste here link, it's hard to remember all the steps of this question. – Ignacio Ocampo Dec 15 '13 at 03:37
  • Thanks again. You've been very helpful and patient with me and I appreciate it. I'll open an new question for this last little tweak. – WJB Dec 15 '13 at 03:47
0

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.

Sir
  • 8,135
  • 17
  • 83
  • 146
  • I did indeed. Thanks very much. It still has not solved it though, as now I am getting an "Access denied for user '###'@'%' to database 'Employees'. I'm pretty sure all credentials are in correct. Further ideas? – WJB Dec 14 '13 at 23:59
  • Firstly check the database is called Employees, notice also your table is also called Employees are you sure they are the same name? Second check your user has the privileges to use that database (check cpanel for that). Cpanel > MySQL databases at the bottom look for `Add User To Database` and add that user to it with all the privalages. – Sir Dec 15 '13 at 00:00
  • THANK YOU SO MUCH!!! It was just the little mistake that I made of not using the database name (instead using the name of the table) Just like you pointed out. Greatly appreciate the patience and help! It's working now! – WJB Dec 15 '13 at 03:27
  • @WJB if the problem is solved please press the tick icon to the left of my answer :)! – Sir Dec 15 '13 at 05:01
  • As soon as my reputation allows I will be coming back and adding points for all of you helpful people. Really appreciate the patience with an obvious new user. :) – WJB Dec 15 '13 at 17:35
  • @WJB you just click the tick and up arrow next to my answer, your rep count does not stop you doing that. – Sir Dec 16 '13 at 00:45