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:36

You have mixed mysqli and mysql, Must use mysqli

Try this

<?php
$host     = "localhost"; // Host name 
$username = "root"; // Mysql username 
$password = ""; // Mysql password 
$db_name  = "test"; // Database name 
$tbl_name = "guestbook"; // Table name 

$con      = mysqli_connect($host, $username, $password, $db_name);

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$datetime = date("y-m-d h:i:s"); //date time
$name     = 'Name here';
$email    = 'example@example.com';
$comment  = 'Comment here';

$sql    = "INSERT INTO $tbl_name (name, email, comment, datetime) VALUES ('$name', '$email', '$comment', '$datetime')";
$result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));

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

mysqli_close($con);
?>
查看更多
该账号已被封号
3楼-- · 2019-03-07 07:38

Should have 2 parameters, the connection link and the database name

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

And you are using mysql with mysqli_* *

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

&

mysql_close();

查看更多
等我变得足够好
4楼-- · 2019-03-07 07:50

Try:

$link = mysqli_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysqli_select_db($link,"$db_name")or die("cannot select DB");
查看更多
疯言疯语
5楼-- · 2019-03-07 07:50

$conn = mysqli_connect($host, $username, $password, $db_name);

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

$result=mysqli_query($conn, $sql);

查看更多
我命由我不由天
6楼-- · 2019-03-07 07:51

I hope it will helps you

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


 $datetime=date("y-m-d h:i:s"); //date time
 $name="abc";
 $email="abc@gmail.com";
  $comment="posted";
  $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name',  '$email', '$comment', '$datetime')";
 $result=mysqli_query($con,$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";
  }
 mysqli_close($con);
  ?>
查看更多
SAY GOODBYE
7楼-- · 2019-03-07 07:52

You have two posibilities one to use a object and one to use a link

With link:

$link = mysqli_connect("$host", "$username", "$password")or die("cannot connect server "); 

mysqli_select_db($link, "$db_name")or die("cannot select DB");

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_row($result);
mysqli_free_result($result);

mysqli_close($link);

Object:

$mysqli = mysqli("$host", "$username", "$password"); 

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->select_db("$db_name");

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result = $mysqli->query($sql);
$row = $result->fetch_row();
$result->close();

$mysqli->close();

More details are describedhere

查看更多
登录 后发表回答