【问题标题】:How to define mysqli connection in one php file and then use it on another file?如何在一个 php 文件中定义 mysqli 连接,然后在另一个文件中使用它?
【发布时间】:2016-06-10 14:51:55
【问题描述】:

我是 PHP 新手,我正在使用 eclipse SDE 构建一个简单的 Web 应用程序。

我的场景是:

  1. 我有 index.php 页面,其中包含登录表单(2 个文本框和登录按钮
  2. 我的 MySQL 数据库包含两个表:system_users 和 user_errors
  3. 当用户成功登录 index.php 页面时,这应该将他重定向到 page1.php,然后从 user_errors 表中检索他的错误并列出它们。

我想在php中实现这个。

下面是我的代码:

index.php

    <html>

<link rel="stylesheet" type="text/css" href="cssDesign.css">

<head>
<title>A BASIC HTML FORM</title>
<?PHP

//MySQL Database Connect 
include 'mysql_connect.php';

$conn = MySQLConnection::getConnection();


// Create connection
//global $con;
//$con = new mysqli("192.168.0.2", "Invest", "NeeD$123","investmentprojects","3306");

if (isset($_POST['Submit1'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

$result = mysqli_query($conn,"SELECT id,username,password,type FROM system_users WHERE username = '".$username."' AND password='".$password."' ");
$rows = mysqli_num_rows($result); 

if ($rows == 1) {
  header('Location: page1.php');
}       
else {
    print ("You're not a member of this site");
    }
}

//mysqli_close($conn);

?>

</head>
<body>

<FORM NAME ="form1" METHOD ="POST" ACTION = "index.php">

<table border="0" style="width:370px;border:0px;">
    <tr> 
        <td style ="width:30%;border:0px;">User</td>
        <td style ="width:70%;;border:0px;"><INPUT style ="width:100%;" TYPE = "TEXT" VALUE ="" NAME = "username"> </td>
    </tr>
    <tr> 
        <td style ="width:30%;border:0px;">Password</td>
        <td style ="width:70%;border:0px;"><INPUT style ="width:100%;" TYPE = "TEXT" VALUE ="" NAME = "password"> </td>
    </tr>
    <tr> 
    <td style ="width:30%;border:0px;"></td>
        <td align='right' style ="width:70%;border:0px;"><INPUT style ="width:30%;" TYPE = "Submit" Name = "Submit1" VALUE = "Login"> </td>
    </tr>

</table>


</FORM>

</body>
</html>

page1.php

    <html>

<link rel="stylesheet" type="text/css" href="cssDesign.css">

<head>
<title>A BASIC HTML FORM</title>
</head>
<body>

<Form name ="form1" Method ="POST" Action ="page1.php">

</FORM>

</body>
</html>

<?php

include 'mysql_connect.php';

$conn = MySQLConnection::getConnection();

$username = $_POST['username'];

$sql = "SELECT user_name,error_form,error_date,error_description " . 
        "FROM error_log WHERE username ='".$username."'";

$result = mysqli_query($conn,$sql);

if ($result->num_rows > 0) {
    echo "<table>";
        echo "<tr>";
        echo "<th>User</th><th>Error Form</th><th>Error Date</th><th>Error Description</th>";
        echo "</tr>";
    // output data of each row

    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>".$row["user_name"]."</td>";
        echo "<td>".$row["error_form"]."</td>";
        echo "<td>".$row["error_date"]."</td>";
        echo "<td>".$row["error_description"]."</td>";
        echo "</tr>";
    }
    echo "</table>";

} else {
    echo "0 results";
}

//mysqli_close($conn);
?>

【问题讨论】:

  • 您遇到的错误是什么?
  • 你卡在哪里了?请解释问题。

标签: php mysql sql database


【解决方案1】:

使用与mysql连接相关的所有文件创建文件connection.php,并在使用mysql的每个文件中的广告之后查询include_once('connection.php');行,或者你可以拥有include_once('some_folder\connection.php');

如果您是初学者,可以使用此模式连接数据库

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect('localhost', 'mysql_user', 'mysql_password');
echo 'Connected successfully';

$res = mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City");

http://php.net/manual/en/mysqli.query.php

【讨论】:

  • include 可能会克隆连接,您应该使用include_once 代替,并且无需添加()
  • 是的,我观察到() 不是必需的,但我使用它是因为对我来说更清楚。如果您为制作一页包含一个文件一次,我认为没有必要编写一次,因为脚本结束并且在新启动后建立新连接(或脚本结束时连接仍然存在???)
  • 连接不会持续(但建议在结束脚本之前断开连接),最好使用include once,它可以在单个代码页或N页上工作。关于(),我同意它更清晰,更少混乱。
  • Mitch 编写的代码是我的案例的半解决方案,因为它生成到数据库的连接。但是,如果此代码保存在“connection.php”文件中,然后在我的“index.php”文件中使用“include(connection.php)”,那么我如何将连接参数传递给 mysqli_query 函数?
  • 使用$link,您将拥有mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City")
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 2014-04-17
  • 2020-07-23
  • 2013-07-19
  • 1970-01-01
  • 2021-07-19
  • 2019-06-23
  • 2017-01-14
相关资源
最近更新 更多