PHP Notes2
PHP Notes2
server
Optional − The host name running database server. If not specified then default value is localhost:3306.
user
Optional − The username accessing the database. If not specified then default is the name of the user that owns the server process.
passwd
Optional − The password of the user accessing the database. If not specified then default is an empty password.
new_link
Optional − If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the
identifier of the already opened connection will be returned.
client_flags
Optional − A combination of the following constants −
•MYSQL_CLIENT_SSL − Use SSL encryption
•MYSQL_CLIENT_COMPRESS − Use compression protocol
•MYSQL_CLIENT_IGNORE_SPACE − Allow space after function names
•MYSQL_CLIENT_INTERACTIVE − Allow interactive timeout seconds of inactivity before closing the connection
• <?php
• $dbhost = 'localhost:3036';
• $dbuser = 'guest';
• $dbpass = 'guest123';
• $conn = mysql_connect($dbhost, $dbuser,
$dbpass);
• if(! $conn ) {
• die('Could not connect: ' . mysql_error());
• }
• echo 'Connected successfully';
• mysql_close($conn);
• ?>
• Using MySQLi object-oriented procedure: We can use the
MySQLi object-oriented procedure to establish a connection
to MySQL database from a PHP script.
• Syntax:
• <?php
• $servername = "localhost";
• $username = "username";
• $password = "password";
• // Creating connection
• $conn = new mysqli($servername, $username, $password);
• // Checking connection
• if ($conn->connect_error) {
• die("Connection failed: " . $conn->connect_error);
•}
• echo "Connected successfully";
• $conn->close();
• ?>
• PHP uses mysql_query function to create a MySQL
database. This function takes two parameters and
returns TRUE on success or FALSE on failure.
• bool mysql_query( sql, connection );