Warning: mysqli_select_db() expects exactly 2 parameters, 1 given *13* [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

This question already has an answer here:

I'm working on a system for my school that lets the teachers post any notices they have for the day on the intranet. However I got this error when testing it out:

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\Users\Matthew\Desktop\wamp64\www\my-site\viewguestbook.php on line 23.

Here is the page's code:

<?php  $host="localhost"; // Host name  $username="root"; // Mysql username  $password=""; // Mysql password  $db_name="test"; // Database name  $tbl_name="guestbook"; // Table name   // Connect to server and select database. mysqli_connect("$host", "$username", "$password")or die("cannot connect server ");  mysqli_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysqli_query($sql); while($rows=mysqli_fetch_array($result)){ ?> 

Note: This is different to the other questions of this same name on the website as it's in a different circumstance.

回答1:

The first parameter of mysqli_select_db() is connection object. This is the syntax :

mysqli_select_db(connection,dbname); 

Change your code to:

$con = mysqli_connect("$host", "$username", "$password")or die("cannot connect server ");  mysqli_select_db($con,$db_name)or die("cannot select DB"); 

Add the connection object as first parameter in mysqli_query and mysqli_fetch_array functions too. Please refer this link for the syntax.



回答2:

Change

mysqli_connect("$host", "$username", "$password")or die("cannot connect server ");  mysqli_select_db("$db_name")or die("cannot select DB"); 

to

 $connection =mysqli_connect("$host", "$username", "$password")or die("cannot connect server ");   mysqli_select_db($connection ,"$db_name")or die("cannot select DB"); 

because mysqli_select_db needs the connection object as well.



回答3:

mysqli_db_select() method requires two parameters

  • connection object

  • database name (that needs to be selected)

So, you should pass connection object to the mysqli_db_select()

    $host="localhost"; // Host name      $username="root"; // Mysql username      $password=""; // Mysql password      $db_name="test"; // Database name      $tbl_name="guestbook"; // Table name       // Connect to server and select database.     $connection = mysqli_connect("$host", "$username", "$password")or die("cannot connect server ");      mysqli_select_db($connection,$db_name)or die("cannot select DB");     $sql="SELECT * FROM $tbl_name";     $result=mysqli_query($connection,$sql);     while($rows=mysqli_fetch_array($result)){ ?> 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!