好的,所以我正在尝试将我的 php 程序/php 文件转换为 .exe 输出,以便我将文件提供给的任何人都可以运行它,无论他们在哪里。它对我来说完美无缺。但是,当我的朋友在他自己的计算机上运行相同的 .exe 文件时,出现一个错误,说明它无法连接到我的数据库。我希望他无需下载其他软件和东西即可访问该程序。我尝试阅读一些可能的解决方案,发现一些建议我应该将其包含在我的数据库连接代码中。他们还使用 Server2go 使其工作:
<?php
// ExeOutput for PHP: MySQL sample using the WAMP package Server2Go
// By default, Server2go comes with a sample database. Root admin is not password-protected.
$mysqlusername = "root";
$mysqlpass = "";
// Do not modify the following lines
$mysqlport = getenv('S2G_MYSQL_PORT');
$mysqlhost = "localhost:".$mysqlport;
// We verify that our ExeOutput application was started by Server2go, otherwise, the MySQL server may not have started.
if (empty($mysqlport)) die("This application cannot be started directly. Programmers: please use the Server2go EXE file, it will start this application automatically.");
?>
问题是我不确定如何将它正确地集成到我目前拥有的连接代码中。错误总是指 $conn = mysqli_connect....部分。这是我现有的连接代码。如何集成建议的解决方案?
<?php
function db_connect()
{
$host = "localhost";
$user = "root";
$password = "";
$database = "csv_db";
$conn = mysqli_connect($host, $user, $password, $database);
if ($conn == FALSE)
{
echo "Error: Unable to connect to the database!";
return NULL;
}
return $conn;
}
function db_disconnect($conn)
{
mysqli_close($conn);
return;
}
function checkUserAccessCookie()
{
/* Check if the user has the "userAccess" cookie (set during login) */
if (isset($_COOKIE["userAccess"]))
{
return true;
}
return false;
}
function getDefaultUserFromCookie()
{
/* If the user has been here before, then a cookie named "userLogin"
* with the user's username will be available. */
if (isset($_COOKIE["userLogin"]))
{
return $_COOKIE["userLogin"];
}
/* If the cookie does not exist, then return blank instead */
return "";
}
?>