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

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

问题:

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.

回答1:

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


回答2:

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



回答3:

Try:

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


回答4:

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


回答5:

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



回答6:

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


回答7:

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



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