How do I use mysql_connect() and mysql_select_db() correctly? How do I use mysql_connect() and mysql_select_db() correctly? nginx nginx

How do I use mysql_connect() and mysql_select_db() correctly?


Your function looks correct, but if you get couldn't make connection, you have your problem there. Localhost is the current host, you can replace it with 127.0.0.1. Did you installed your mysql server already? I guess you have not so do first this:

apt-get install mysql-server mysql-client php5-mysql

After that you can install phpmyadmin as well, then it's easier for you to handle your databases:

apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin

Now you can create your database.

Here is a quick video, that can probably help you:http://www.youtube.com/watch?v=N_c018_lhHQ

(try to use the mysqli or pdo API, both are easier than the old mysql API.


localhost is an alias for the loopback IP address 127.0.0.1. In other words, localhost is another word for "this computer" (i.e. the computer you are sitting at right now).

define('DB_HOST', 'localhost'); // Points back to this computer

The username and password values in your code need to be fixed. First of all, strings have to be enclosed in quotes (single or double). Second, in order to get PHP's INI settings you need to use the ini_get() function:

define('DB_USER', ini_get('mysql.default_username'));define('DB_PASS', ini_get('mysql.default_password'));

The database name is the name of the database (!) that you want to work with. If you use phpMyAdmin then there should be a dropdown menu on the left side of the screen where you can select databases. The names in that dropdown menu are also the names you need to use in your PHP scripts.

define('DB_NAME', 'database_name_i_dunno');

These look to be okay.

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database.");