Php MySQL连接检查来自';表格';使用$GET[';';];和数组

Php MySQL连接检查来自';表格';使用$GET[';';];和数组,php,html,mysql,Php,Html,Mysql,尝试连接到MySQL并通过我创建的表单检查连接。我正在使用数组存储变量并将它们传递给mysql_query(),我得到的结果是 无法连接到数据库 顺便说一句,以前有人告诉我使用PDO,但我不知道如何使用阵列建立PDO连接 我试过: echo$\u获取['hostname']-它回显表单字段和其他字段 检查语法 我还把$\uCode>GET['hostname']、$\uGet['username']、$\uGet['password']$\uGet['database']自身编码到mysql_

尝试连接到MySQL并通过我创建的表单检查连接。我正在使用数组存储变量并将它们传递给mysql_query(),我得到的结果是

无法连接到数据库

顺便说一句,以前有人告诉我使用PDO,但我不知道如何使用阵列建立PDO连接

我试过:

  • echo
    $\u获取['hostname']-它回显表单字段和其他字段
  • 检查语法
  • 我还把$\uCode>GET['hostname']、$\uGet['username']、$\uGet['password']$\uGet['database']自身编码到mysql_query()中;也没有运气
关注点:
顺便问一下,有人知道你的phpmyadmin是否位于mac上的/~username中吗?它在第二个或第三个目录中仍然有用吗

示例:

~/username/app/index.php

Index.php

<form action="checkconnection.php" method="POST">
<label>Hostname:</label></span><br /><input autofocus="autofocus" placeholder="examples: (localhost etc)" name="hostname" id="hostname" type="text" /><br />
<label>Username:</label><br /><input name="username" id="username" type="text" /><br />
<label>Password:</label><br /><input name="password" id="password" type="text" /><br />
<label>Database:</label><br /><input placeholder="Your Database Name" name="database" id="database" type="text" /><br />
<button class="continue">Continue</button>
<button class="CheckConnection">Check Connection</button>
<button type="reset" class="reset">Reset</button>
</form>
<?php 

/*Database Checking and Result send back to Installation */
$dbLogin = array(
    'Hostname' => $_POST["hostname"],
    'Username' => $_POST["username"],
    'Password' => $_POST["password"],
    'Database' => $_POST["database"]
    );


$databaseConnection = mysql_query($dbLogin['Hostname'], $dbLogin['Username'], $dbLogin['Password'], $dbLogin['Database']);

if ($databaseConnection) {
    echo "Yes, your connection has been established.";
}else {

    echo "Failed to connect to Database.";
}

?>

主机名:

用户名:

密码:

数据库:

持续 检查连接 重置
checkconnection.php

<form action="checkconnection.php" method="POST">
<label>Hostname:</label></span><br /><input autofocus="autofocus" placeholder="examples: (localhost etc)" name="hostname" id="hostname" type="text" /><br />
<label>Username:</label><br /><input name="username" id="username" type="text" /><br />
<label>Password:</label><br /><input name="password" id="password" type="text" /><br />
<label>Database:</label><br /><input placeholder="Your Database Name" name="database" id="database" type="text" /><br />
<button class="continue">Continue</button>
<button class="CheckConnection">Check Connection</button>
<button type="reset" class="reset">Reset</button>
</form>
<?php 

/*Database Checking and Result send back to Installation */
$dbLogin = array(
    'Hostname' => $_POST["hostname"],
    'Username' => $_POST["username"],
    'Password' => $_POST["password"],
    'Database' => $_POST["database"]
    );


$databaseConnection = mysql_query($dbLogin['Hostname'], $dbLogin['Username'], $dbLogin['Password'], $dbLogin['Database']);

if ($databaseConnection) {
    echo "Yes, your connection has been established.";
}else {

    echo "Failed to connect to Database.";
}

?>

首先,确保使用MySQLi或PDO,因为MySQL已经过时,并且存在一些安全问题

然后,替换

echo "Failed to connect to Database.";


如果仍然不起作用,请在评论中回复:)

您可以将其放在连接语法之后。如果您有任何与数据库连接相关的错误,那么它将显示该错误

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysql_connect_error();
}
查查

mysql\u connect()
-

mysql\u选择\u db()
-


mysql\u query()
-

replace
mysql\u query($dbLogin…
使用
mysqli_connect
Wow在我之前建立了这么多连接之后,看看我的问题有多小..谢谢man lol@FuzzyTreeI我尝试了echo$databaseConnection->connect_errno;看看如果我的东西错了,我会得到什么,它实际上会把我扔到空白页而不是一些错误如果你成功连接,它会我不会给你一个错误:)是的,我知道,它会给我是的,你的连接已经建立。但是如果我连接失败了怎么办?我尝试了,但它仍然没有给我错误好的,尝试
echo$databaseConnection->error;
。我认为这不值得一试。我以前给你的代码应该可以工作。我已经解决了这个问题(感谢@FuzzyTree),通过更改为mysqli_connect();,所以我猜缓存错误应该是
mysqli_connect_error();
但感谢您的长期帮助,加油!:)