PHP functions for MySql : mysqli_select_db()

The mysqli_select_db() function is used to select or change the default database for the connection. The syntax is
mysqli_select_db(connection, dbname);
Where :
  • connection specifies the MysQL connection to use.
  • dbname specifies the name of database.
Example :
<?php

  $server = "localhost";
  $username = "root";
  $password = "";

  $connection = mysqli_connect($server, $username, $password);

  // Select the newly created database
  mysqli_select_db($connection, "mydb");

  $sql = "SELECT * from employee";
  $result = mysqli_query($connection, $sql);

  $row = mysqli_fetch_all($result, MYSQLI_ASSOC);
  $rowcount = mysqli_num_rows($result);

  // iterate through each row
  for($i = 0; $i < $rowcount; $i++) {
    echo $row[$i]["EmpId"] . " : " . $row[$i]["EmpName"] . "<br/>";
  }

  mysqli_close($connection);

?>
Output :

101 : John
102 : Mary
103 : Sam
104 : Bruce
105 : Clark Kent


Next Topic :