-5

I am trying to teach myself PHP and I've come unstuck at a very early stage, I'm trying to connect to my db with the following code:

$server = 'server';
$username = 'username';
$password = 'password';
$database = 'database';

if(!mysqli_connect($server, $username,  $password))
{
exit('Error: could not establish database connection');
}
if(!mysqli_select_db($database)
{
exit('Error: could not select the database');
}

However, when I run this code I get the following error:

Parse error: syntax error, unexpected '{' in connect.php on line 22

If I comment out the final if statement the error goes away.

I have tried Google etc, but I feel that my lack of knowledge/experience means that I might not recognise the solution just from scouring random search results.

As a side note, Visual Studio is also giving me a sytax error on the final semi colon.

2 Answers2

0

Are you missing a bracket in your last if statement?

if(!mysqli_select_db($database)

should be

if(!mysqli_select_db($database))

P.S. Try use an editor that warns you of these bracket errors. Like when you select one bracket it highlights the pairing bracket or groups code blocks. This helps finds time consuming bugs like this.

micstr
  • 5,080
  • 8
  • 48
  • 76
0

Typos:

if(!mysqli_select_db($database)
  ^--open #1        ^--open #2
                              ^---close #2 
}

you never close #1... e.g. you're missing a ).

Marc B
  • 356,200
  • 43
  • 426
  • 500