Get Website Learn Coding Tech Story

PHP Programming Examples

<?php
	/* Your mysql server host name */
	$host = "localhost";
	
	/* Mysql database user name */
	$user = "root";
	
	/* Mysql database user password */
	$pass = "yourpass";
	
	/* Open connection mysql server in mysqli procedural */
	$conn = mysqli_connect($host,$user,$pass);
	
	if($conn) {
		/* Select database */
		$result = mysqli_select_db($conn, "test");
		if($result)
			echo "Database selected and ready for query!";
		else
			echo "Error to select database!";
	}
	else
		echo "Connection to the Server could not be established!";
	
	/* Close mysqli connect */
	mysqli_close($conn);
		
	
	/* Open connection mysql server in mysqli object oriented */
	$conn = new mysqli($host,$user,$pass) or die("Connection to the Server could not be established!");
	
	if($conn) {
		
		/* Select database */
		$result = $conn->select_db("test");
		if($result)
			echo "Database selected and ready for query!";
		else
			echo "Error to select database!";
	}
	
	/* Close mysqli connect */
	$conn->close();
?> 


          /****** Output ******/
          

Database selected and ready for query!

Learn more about mysqli select db