我正在使用WAMP服务器并在其上创建了一个数据库和一个表。所有名称都是正确的,用户可以完全访问所有内容。当我运行代码时,它会输出"无法选择数据库"。感谢。
<?php
if(isset($_POST["Submit"])){
print_r ($_POST["nutrient"]);
}
session_start();
//establish connection
$server = "localhost";
$db_username = "root";
$db_password = "";
$database = "gainlife_cavin";
$table = "cavintable";
//connect PHP script to database
$connection = mysqli_connect($server, $db_username, $db_password, $database);
//select database to use
@mysql_select_db($database) or die( "Unable to select database");
//$query = "INSERT INTO $table VALUES("")"
//mysql_query($query)
mysql_close();
?>
<body>
</form>
答案 0 :(得分:1)
尝试以下内容。
<?php
//establish connection
$server = "localhost";
$db_username = "root";
$db_password = "";
$database = "gainlife_cavin";
$table = "cavintable";
//connect PHP script to database
$connection =mysqli_connect("$server","$db_username","$db_password","$database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Your query here
mysqli_close($connection);
?>
答案 1 :(得分:0)
我使用简单的代码1行。这是我正在使用的代码。
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
答案 2 :(得分:-1)
你从MySQLI到选择数据库以及你的其余代码都没有使用MySQLI。
答案 3 :(得分:-2)
试试这个。我已将mysqli_connect
更改为mysql_connect
和mysql_select_db
变量。
//connect PHP script to database
$connection = mysql_connect($server, $db_username, $db_password, $database);
//select database to use
$select = mysql_select_db($connection) or die( "Unable to select database");