-4

Hi i need some help in coding php to connect database my source code is

$host="127.0.0.1"; // Host name 

$username="root"; // Mysql username 

$password=""; // Mysql password 

$db_name="test"; // Database name 

$tbl_name="forum_question"; // Table name 

// Connect to server and select databse.
`mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
mysqli_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";`

But it display error

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\123\tryforum\main_forum.php on line 11 cannot select DB

How to solve it

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143

2 Answers2

0

You need to pass connection variable to mysqli_select_db. See this link

$host="127.0.0.1"; // Host name 

$username="root"; // Mysql username 

$password=""; // Mysql password 

$db_name="test"; // Database name 

$tbl_name="forum_question"; // Table name 

// Connect to server and select databse.
$con = mysqli_connect("$host", "$username", "$password") or die("cannot connect"); 
mysqli_select_db($con, "$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";`
Suyog
  • 2,472
  • 1
  • 14
  • 27
0

mysqli_select_db()

needs two parameter first your database connection and second your database name

$conn=mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
mysqli_select_db($conn,"$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysqli_query($conn,$sql); 
Saty
  • 22,443
  • 7
  • 33
  • 51