警告:mysqli_select_db()需要2个参数,给定1个

时间:2016-04-08 05:39:01

标签: php mysql mysqli

我正在为我的学校制作一个系统,让教师在内联网上发布他们当天的任何通知。我正在关注this教程,更改代码以满足我的需求,但是在测试时遇到了这个错误:

  

警告:mysqli_select_db()预计在第15行的C:\ Users \ Matthew \ Desktop \ wamp64 \ www \ my-site \ addguestbook.php中只有2个参数,1。

以下是该页面的代码:

    <?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();
?>

注意:这与网站上同名的其他问题不同,因为它处于不同的情况。

7 个答案:

答案 0 :(得分:1)

你有一个使用对象和一个使用链接的可能性

使用链接:

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

对象:

$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();

详细说明here

答案 1 :(得分:0)

而不是

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

答案 2 :(得分:0)

尝试:

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

答案 3 :(得分:0)

应该有2个参数,连接链接和数据库名称

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

你正在使用mysql与mysqli_ * *

  

$ sql =&#34; INSERT INTO $ tbl_name(姓名,电子邮件,评论,   datetime)VALUES(&#39; $ name&#39;,&#39; $ email&#39;,&#39; $ comment&#39;,&#39; $ datetime&#39;)&#34 ;;   $结果= mysql_query($ SQL);

&安培;

  

mysql_close();

答案 4 :(得分:0)

我希望它会对你有所帮助      

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

答案 5 :(得分:0)

你混合了mysqli和mysql,必须使用mysqli

试试这个

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

答案 6 :(得分:0)

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