user3289991
user3289991

Reputation: 29

Can't make a connection to a db thru php

This is my first time mixing PHP and SQL, and my problem is that I cannot connect to my database with PHP. I am using mysql_connect (and I've also found mysqli_connect, I can't see the difference).

This is my code:

mysql_connect("localhost", "username", "password","db_name");

I have put it between < ?php and a ?> in my index.php file.
And all it has showed me so far is this:

Parse error: syntax error, unexpected 'mysql_connect' (T_STRING) in /home/username/domains/website.com/public_html/index.php on line 63

I can't see the problem since I've been doing everything according to W3C's tutorial.

Upvotes: 0

Views: 46

Answers (1)

aviel
aviel

Reputation: 188

You must assign the return of mysql_connect !

$link = mysql_connect(...);

If you need to do database connection it's time to take a look at PHP documentation : https://www.php.net/manual/en/function.mysql-connect.php

Warning

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

mysqli_connect()
PDO::__construct()

If you're starting a project I think you haven't set all your queries statements and it'd be a great idea to change to mysqli or pdo now. Migrating parts of code is always a pain since you need to do all application tests for non regression.

Upvotes: 1

Related Questions