如何从另一个文件 php 验证 mysql 数据库连接

我创建了一个表单,用户可以在其中添加他的数据库名称,用户名,密码,主机。然后该表单创建一个配置文件并添加用户数据库详细信息。表单位于根目录中,配置文件位于根/包含中。我想知道如何从表单文件验证数据库连接。如果连接成功,他可以继续下一步,如果不是,我想显示错误。


形式


if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['db-details']) {


    $dbName = $_POST['dbName'];

    $dbUsername = $_POST['dbUsername'];

    $dbPassword = $_POST['dbPassword'];

    $dbHost = $_POST['dbHost'];


    $phConfigFile = 'includes/ph_config.php';


    file_put_contents($phConfigFile, $phConfigData);


}

配置


<?php 


    define("DB_HOST", "[dbHost]");


    define("DB_USER", "[dbUsername]");


    define("DB_PASS", "[dbPassword]");


    define("DB_NAME", "[dbName]");


    $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);


紫衣仙女
浏览 96回答 3
3回答

蛊毒传说

您确实想在创建配置文件之前检查连接,因此一旦创建,您就知道它将起作用if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['db-details']) {&nbsp; &nbsp; $dbName&nbsp; &nbsp; &nbsp;= $_POST['dbName'];&nbsp; &nbsp; $dbUsername = $_POST['dbUsername'];&nbsp; &nbsp; $dbPassword = $_POST['dbPassword'];&nbsp; &nbsp; $dbHost&nbsp; &nbsp; &nbsp;= $_POST['dbHost'];&nbsp; &nbsp; // use error supression so you get to process the error rather&nbsp;&nbsp; &nbsp; // than PHP throwing an error&nbsp; &nbsp; $connection = @mysqli_connect($_POST['dbHost'],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST['dbUsername'],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST['dbPassword'],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST['dbName']);&nbsp; &nbsp; if ( ! $connection ) {&nbsp; &nbsp; &nbsp; &nbsp; // do whatever you need to when the information passed does not work&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }&nbsp; &nbsp; // must be valid as we connected&nbsp; &nbsp; $str = '<?php' . PHP_EOL;&nbsp; &nbsp; $str .= 'define("DB_HOST", "' . $_POST['dbHost'] . '");'&nbsp; &nbsp; &nbsp; &nbsp; . PHP_EOL;&nbsp; &nbsp; $str .= 'define("DB_USER", "' . $_POST['dbUsername'] . '");'&nbsp; &nbsp; . PHP_EOL;&nbsp; &nbsp; $str .= 'define("DB_PASS", "' . $_POST['dbPassword'] . '");'&nbsp; &nbsp; . PHP_EOL;&nbsp; &nbsp; $str .= 'define("DB_NAME", "' . $_POST['dbName'] . '");'&nbsp; &nbsp; &nbsp; &nbsp; . PHP_EOL;&nbsp;&nbsp; &nbsp; $str .= '$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);' . PHP_EOL;&nbsp; &nbsp; // create the config file in all confidence that the values will work&nbsp; &nbsp; file_put_contents($phConfigFile, $str);&nbsp; &nbsp;&nbsp;}

小怪兽爱吃肉

这与里格斯弗利的答案相同,但有所改进。if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['db-details'])) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);&nbsp; &nbsp; &nbsp; &nbsp; $connection = new mysqli(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST['dbHost'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST['dbUsername'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST['dbPassword'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST['dbName']&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $DBconfig = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dbHost' => $_POST['dbHost'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dbUsername' => $_POST['dbUsername'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dbPassword' => $_POST['dbPassword'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dbName' => $_POST['dbName']&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; // generate the config file&nbsp; &nbsp; &nbsp; &nbsp; $contents = '<?php' . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; $contents .= '$DBconfig = ' . var_export($DBconfig, true) . ';' . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; $contents .= 'mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);' . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; $contents .= '$connection = new mysqli(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $DBconfig["dbHost"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $DBconfig["dbUsername"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $DBconfig["dbPassword"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $DBconfig["dbName"]&nbsp; &nbsp; &nbsp; &nbsp; );';&nbsp; &nbsp; &nbsp; &nbsp; echo $contents;&nbsp; &nbsp; } catch (\Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; // echo error message here and show the same form.&nbsp;&nbsp; &nbsp; }}基本上,您应该不惜一切代价避免错误抑制运算符。值得庆幸的是,PHP有异常,可以捕获和处理。如果没有异常,则可以将连接详细信息放在数组中,然后将该数组导出为字符串。生成连接文件并保存。我要改进的另一件事是将其封装在函数中,以防止配置细节溢出。// generate the config file$contents = '<?php' . PHP_EOL;$contents .= 'function db_connect():\mysqli { '. PHP_EOL;$contents .= '$DBconfig = ' . var_export($DBconfig, true) . ';' . PHP_EOL;$contents .= 'mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);' . PHP_EOL;$contents .= 'return new mysqli(&nbsp; &nbsp; $DBconfig["dbHost"],&nbsp; &nbsp; $DBconfig["dbUsername"],&nbsp; &nbsp; $DBconfig["dbPassword"],&nbsp; &nbsp; $DBconfig["dbName"]);';$contents .= PHP_EOL;$contents .= '}'.PHP_EOL;$contents .= '$connection = db_connect();'.PHP_EOL;echo $contents;

鸿蒙传说

因此,如果我答对了,您想知道用户输入的数据是否实际上有效以连接到服务器。未验证它。您可以包含刚刚编写的文件并对其进行测试。只需确保在包含后使用相同的变量名称即可。if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['db-details']) {&nbsp;$dbName = $_POST['dbName'];&nbsp;$dbUsername = $_POST['dbUsername'];&nbsp;$dbPassword = $_POST['dbPassword'];&nbsp;$dbHost = $_POST['dbHost'];&nbsp;$phConfigFile = 'includes/ph_config.php';&nbsp;file_put_contents($phConfigFile, $phConfigData);&nbsp;include($phConfigFile);&nbsp;if ($connection->connect_errno) {&nbsp; //data for the connection isn't valid&nbsp; echo "Failed to connect to MySQL: " . $connection->connect_error;&nbsp; exit();&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP