0

This is my first time using PHP to read/write to a MySQL database and I'm stuck on step one. I am following the w3schools guide and created a php file named "welcome.php" which I can view by navigating to localhost/dbpopulate/welcome.php.

The HTML / PHP for this is:

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "user";
$password = "default";

try {
    $conn = new PDO("mysql:host=$servername;dbname=user", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?> 


</body>
</html> 

I can access this 'user' database using the 'default' password without issue from inside and outside of my network on a multitude of devices. When I attempt to access the page, I receive this error:

Parse error: syntax error, unexpected '$conn' (T_VARIABLE) in C:\wamp64\www\DBPopulate\welcome.php on line 11

Line 11 is:

$conn = new PDO("mysql:host=$servername;dbname=user", $username, $password);

Thanks!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Eric
  • 29
  • 5
  • 1
    `dbname=user` are you sure of the database name? and that the code you posted is in fact that? I can't see this failing. Make sure there's no hidden unicode also. – Funk Forty Niner Aug 14 '17 at 00:36
  • 1
    You would normally get `syntax error, unexpected '$whatever'` is you have not ended a previous line with a semicolon, or have not closed quotes. Neither is the case here... Double-check in your actual code, tho. – Aydin4ik Aug 14 '17 at 00:41
  • I don't think you're telling us the whole story here and how else you're using this. Voting to close as unclear. – Funk Forty Niner Aug 14 '17 at 00:44
  • This is quite literally it. The username is five digit number that matches the name of the database. The password is default. The host is localhost (have also tried 127.0.0.1). – Eric Aug 14 '17 at 00:52
  • 1
    Triple-check your code. You surely have a stray character or perhaps a "smart quote" in your actual code. The code you have given here would not produce the error you are getting. – elixenide Aug 14 '17 at 00:57
  • Oh, and you absolutely should not allow remote access without an encrypted connection, and even then not with the password "default". That is begging to get your machine hacked. – elixenide Aug 14 '17 at 00:59
  • I understand that, this is not in production / even close to it. I am just simply trying to open a connection to a MySQL database. I went ahead and created a new user '12345' with the password 'default', from host set to '%' and that one does not work either. I'm not hiding anything and this is simply exactly what I posted :(. – Eric Aug 14 '17 at 01:05
  • 1
    @Fred-ii-, thank you for the suggestion about hidden Unicode, there were characters present on the copy from w3schools that I identified in NetBeans. Appreciate the help. – Eric Aug 14 '17 at 01:31
  • @Eric Aahhh, I had a Spidey sense about that. Glad I was able to help, *cheers* and you're welcome. – Funk Forty Niner Aug 14 '17 at 01:48
  • @EdCottrell Seems it was a unicode character after all (see new comments) and the duplicate you chose to close the question with, doesn't cover that. – Funk Forty Niner Aug 14 '17 at 01:52
  • @Fred-ii- I disagree; the [relevant answer](https://stackoverflow.com/a/15539535/2057919) in the duplicate still explains the error, and this text is still valid: "A problem with the syntax of your program, such as leaving a semicolon off of the end of a statement or, like the case above, missing the `.` operator." In any case, there are many questions here with solutions addressing this problem. I've added a couple as additional duplicates. [This one](https://stackoverflow.com/a/18050072/2057919) in particular is helpful and addresses invisible unicode characters. – elixenide Aug 14 '17 at 02:07
  • @EdCottrell [The second link](https://stackoverflow.com/a/18050072/2057919) is indeed the correct one. Thanks for finding the right one Ed, *cheers* – Funk Forty Niner Aug 14 '17 at 02:16

0 Answers0