[英]Can't connect to localhost mysql database
I'm trying to make a simple form that can be stored on mysql database. 我正在尝试制作一个可以存储在mysql数据库上的简单表格。 Whenever I try to connect it won't let me.
每当我尝试连接时,都不会允许我。 Here's what I have:
这是我所拥有的:
PHP: PHP:
<?php
define('DB_NAME', 'forms1');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
$db_selected = mysqli_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysqli_error());
}
$value = $_POST['input1'];
$value2 = $_POST['input2'];
$sql = "INSERT INTO demo (input1, input2) VALUES ('$value', '$value2')";
if (!mysqli_query($sql)) {
die('Error: ' . mysqli_error());
}
mysqli_close();
?>
HTML: HTML:
<form action="demo.php" method="post" />
<p>Input 1: <input type="text" name="input1" /></p>
<p>Input 2: <input type="text" name="input2" /></p>
<input type="submit" value="Submit" />
</form>
I the correct credentials, but when I open up my page it says this, "Can't use forms1:" 我输入了正确的凭据,但是当我打开页面时它说:“不能使用forms1:”
Doesn't even give me the error. 甚至都没有给我错误。
It will give you an error message if you add the connection variable to the call to mysqli_error()
如果将连接变量添加到对
mysqli_error()
的调用中,它将给您一条错误消息。
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysqli_error($link));
}
This applied to all calls to mysqli_error($link)
这适用于所有对
mysqli_error($link)
调用
This mistake should have generated a PHP error, so if you did not see that I assume you are developing on a LIVE server where error reporting is turned off. 此错误应该产生了PHP错误,因此,如果您没有看到我假设您正在关闭错误报告的LIVE服务器上进行开发。 In that case you should start all your scripts like this
在这种情况下,您应该像这样启动所有脚本
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
This will ensure that you see these errors on the browser! 这样可以确保您在浏览器中看到这些错误! Once a script is debugged you should probably remove these lines as you dont want a user to see this information should something go wrong while it is a LIVE site
脚本调试完毕后,您可能应该删除这些行,因为如果它是LIVE站点时出现问题,您不希望用户看到此信息。
Also the connect should contain the database name, 连接也应包含数据库名称,
<?php
define('DB_NAME', 'forms1');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
//$db_selected = mysqli_select_db(DB_NAME, $link);
//if (!$db_selected) {
// die('Can\'t use ' . DB_NAME . ': ' . mysqli_error());
//}
The mysqli_select_db()
call is there so you can change to another database on the same connection if you were using 2 databases for some reason. 那里有
mysqli_select_db()
调用,因此如果出于某种原因使用2个数据库,则可以在同一连接上更改为另一个数据库。
I assume you are converting from the
mysql_
API tomysqli_
API unfortunately the convertion is not a one to one conversion.我假设您正在从
mysql_
API转换为mysqli_
API,不幸的是,该转换不是一对一转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.