嗨我有一个sql错误说:没有数据库选择这是我的KK.php文件:
$user_name = "root";
$password = "";
$database = "MyDB";
$server = "localhost";
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$message = mysqli_real_escape_string($link, $_POST['input1']);
$sql="INSERT INTO demo (message)
VALUES ('$message')";
if (!mysqli_query($link,$sql)) {
die('Error: ' . mysqli_error($link));
}
echo "1 record added";
mysqli_close($link);
?>
我的数据库是“MyDB”,还有我的demo.php文件:
<form action="KK.php" method="post" />
<p>Message: <input type="text" name="input1" /></p>
<input type="submit" value="Submit" />
</form>
那么问题是什么?,我如何选择数据库?
答案 0 :(得分:2)
您是否忘记调用mysqli_connect来获取连接?
即:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
答案 1 :(得分:1)
除了我上面的评论,这里还有你缺少的代码。
$user_name = "root";
$password = "";
$database = "MyDB";
$server = "localhost";
// This
$link = mysqli_connect($server,$user_name,$password,$database); // This
// This
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
答案 2 :(得分:0)
您没有致电mysqli_connect
$con = mysqli_connect("localhost","my_user","my_password","my_db");