Warning: mysqli_select_db() expects exactly 2 para

2019-03-07 07:19发布

I'm working on a system for my school that lets the teachers post any notices they have for the day on the intranet. I'm following this tutorial, changing the code to suit my needs, 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\addguestbook.php on line 15.

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");

$datetime=date("y-m-d h:i:s"); //date time

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);

//check if query successful 
if($result){
echo "Successful";
echo "<BR>";

// link to view guestbook page
echo "<a href='viewguestbook.php'>View guestbook</a>";
}

else {
echo "ERROR";
}
mysql_close();
?>

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

标签: php mysql mysqli
7条回答
戒情不戒烟
2楼-- · 2019-03-07 07:53

Instead of

// 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");

do

// Connect to server and select database.
$db=mysqli_connect($host, $username, $password)or die("cannot connect server "); 
mysqli_select_db($db,$db_name)or die("cannot select DB");
查看更多
登录 后发表回答