如何定义mysqli连接在一个php文件,然后在另一个文件使用它? [英] How to define mysqli connection in one php file and then use it on another file?

查看:143
本文介绍了如何定义mysqli连接在一个php文件,然后在另一个文件使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的方案是:


$

我使用eclipse SDE来构建一个简单的web应用程序。 b $ b


  1. 我有 index.php 页面,其中包含登录表单( 2文本框和登录按钮

  2. 我有MySQL数据库,其中包含两个表:system_users& user_errors

  3. 每当用户在index.php页面成功登录时,应将他重定向到 page1.php ,然后从user_errors表中检索其错误列出他们。

我想在php中实现。



是我的代码:



index.php

 < html& 

< link rel =stylesheettype =text / csshref =cssDesign.css>

< head>
< title>基本HTML表单< / title>
<?PHP

// MySQL数据库连接
include'mysql_connect.php';

$ conn = MySQLConnection :: getConnection();


//创建连接
//全局$ 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(您不是本网站的成员);
}
}

// mysqli_close($ conn);

?>

< / head>
< body>

< FORM NAME =form1METHOD =POSTACTION =index.php>

< table border =0style =width:370px; border:0px;>
< tr>
< td style =width:30%; border:0px;> User< / td>
< td style =width:70%;; border:0px;>< INPUT style =width:100%; TYPE =TEXTVALUE =NAME =username> < / td>
< / tr>
< tr>
< td style =width:30%; border:0px;>密码< / td>
< td style =width:70%; border:0px;>< INPUT style =width:100%; TYPE =TEXTVALUE =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 =SubmitName =Submit1VALUE =登录> < / td>
< / tr>

< / table>


< / FORM>

< / body>
< / html>

page1.php

 < html> 

< link rel =stylesheettype =text / csshref =cssDesign.css>

< head>
< title>基本HTML表单< / title>
< / head>
< body>

< Form name =form1Method =POSTAction =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>用户< / th>< th>错误表单< / th>< th>错误日期< / th>< th>错误说明&
echo< / tr>;
//每行的输出数据

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 {
echo0 results;
}

// mysqli_close($ conn);
?>


解决方案

使文件connection.php与所有mysql连接和每个文件中使用mysql查询行 include('connection.php'); 或者你可以有 include some_folder\connection.php');



如果您是初学者,可以使用此模式连接到DB

  $ link = mysqli_connect('localhost','mysql_user','mysql_password'); 
if(!$ link){
die('Could not connect:'。mysqli_error());
}
echo'Connected successfully';


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




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



I am new to PHP and I`m using eclipse SDE to build a simple web application.

My Scenario is:

  1. I have index.php page that include the login form (2 textbox and Login Button)
  2. I have MySQL database that contains two tables: system_users & user_errors
  3. whenever a user login successfully in the index.php page, this should redirect him to the page1.php and then retrieve his errors from the user_errors table and list them.

I want to achieve this in php.

Hereafter is my code:

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);
?>

解决方案

Make file connection.php with all that have tangence with mysql connection and after ad in each file where you use mysql query the line include('connection.php'); or you can have include('some_folder\connection.php');

If you are beginer you can use this mode to connect to DB

 $link = mysqli_connect('localhost', 'mysql_user', 'mysql_password');
 if (!$link) {
   die('Could not connect: ' . mysqli_error());
 }
 echo 'Connected successfully';


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

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

这篇关于如何定义mysqli连接在一个php文件,然后在另一个文件使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆