在php中连接到mysql数据库

I am trying an example tutorial for AJAX suggest from here. I found an issue in the database.php file. it containts a code segment like this.

function db_connect($server = 'localhost', $username = 'root', $password = '123', $database = 'db_ajax_suggest', $link = 'db_link') {
    global $$link;

    $$link = mysql_connect($server, $username, $password);

    if ($$link) mysql_select_db($database);

    return $$link;
  }

My question is what is $link = 'db_link' in the code. I have worked with php before, but not a lot, I have not met a situation like this. All needed were $servername, $username, $password.

I am a java person more than PHP. In java I can use jdbc:mysql://localhost:3306/db_ajax_suggest. But in this case, Hoe should set the database url? Thank you!!

I think Only this much you needed to connect to your databases and work with your tables

$connect = mysql_connect('localhost','root','123');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db('db_ajax_suggest',$connect);

and for fetching data from table

$query = " your query ";
mysql_query($query) or die('SQL ERROR:'.mysql_error());

Write

mysql_connect($server, $username, $password) or die(mysql_error());

and for DB

mysql_select_db($database) or die(mysql_error());

and for your queries

mysql_query($q) or die(mysql_error());

I find nothing better solution than this.

mysql_connect can have four paramter.

like this

mysql_connect($server,$user,$pwd,$newlink,$clientflag)

In my my first three you already know. You might have confusiong about $newlink and $clientflag

$newlink= Return bool value means TRUE or FALSE.

Usage: If the connection is called again in the script. now new conection will be created. if its set to TRUE.

clientflag The client_flags parameter can be a combination of the following constants: 128 (enable LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE. Read the section about MySQL client constants for further information. In SQL safe mode, this parameter is ignored. **according to the php manual**

Check these link for further info

http://php.net/manual/en/function.mysql-connect.php

http://php.net/manual/en/mysql.constants.php#mysql.client-flags

Important Note: Please learn mysqli or PDO