php database connection

mysql connect to database PHP

  • October 19, 2018

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

We realize that there are loads of instructional exercises (some authority as well) on the web about PHP and MySQL network. At that point for what reason am I posting it here? I am posting on the grounds that the vast majority of PHP database connection are not basic and straightforward and this is the reason we are doing it here. We expect that you have just introduced XAMPP in your System and running it utilizing XAMPP control board.

To associate php database connection database you have to know the accompanying essential things:

  1. Hostname.
  2. MySQL client name.
  3. MySQL secret phrase.

Associating with a database by means of PHP is a critical advance supposing that your content can’t interface with its database, your inquiries to the PHP database connection will come up short.

Description

A decent practice when utilizing databases is to set the username, the secret key and the database name esteems toward the start of the content code. In PHP database connection that you have to transform them later, along these lines you will most likely play out the change effectively.

This instructional exercise will be exceptionally helpful for you in the event that you are beginning to find out about PHP database connection advancement. Utilizing this instructional exercise, you will figure out how to utilize PHP to associate with MySQL database.

resource mysql_connect ([ string $server = ini_get(“mysql.default_host”) [, string $username = ini_get(“mysql.default_user”) [, string $password = ini_get(“mysql.default_password”) [, bool $new_link = FALSE [, int $client_flags = 0 ]]]]] )

Parameters

Server

The MySQL server. It can also include a port number. e.g. “hostname:port” or a path to a local socket e.g. “:/path/to/socket” for the localhost.

If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.

Username

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

The username. Default value is defined by mysql.default_user. In SQL safe mode, this parameter is ignored and the name of the user that owns the server process is used.

Password

The default value is defined by MySql.default_password. In SQL safe mode, this parameter is ignored and null password is used.

New_link

If a second call is made to mysql_connect() with the same arguments or php database connection , no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.
client_flags

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.

Examples

mysql_connect() example

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

mysql_connect() example using hostname:port syntax

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

mysql_connect() example using “:/path/to/socket” syntax

<?php
// we connect to localhost and socket e.g. /tmp/mysql.sock

// variant 1: omit localhost
$link = mysql_connect(':/tmp/mysql', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);


// variant 2: with localhost
$link = mysql_connect('localhost:/tmp/mysql.sock', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Visit our php operator article

Tech Friend

E-mail : info@phplab.in

Submit A Comment

Must be fill required * marked fields.

:*
:*