$link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);
时间: 2024-01-01 12:03:25 浏览: 26
这是一个使用 PHP 语言连接 MySQL 数据库的代码片段。
- `mysql_connect()` 函数用于连接 MySQL 数据库。
- `{$host}:{$port}` 表示 MySQL 主机和端口号,`$host` 是主机名,`$port` 是端口号。
- `$user` 和 `$pwd` 分别是 MySQL 数据库的用户名和密码。
- 最后一个参数 `true` 表示使用持久化连接,即在脚本执行完后不会关闭数据库连接,以便下一次连接时可以直接使用已经建立的连接,从而提高了脚本的性能。
- `@` 符号表示抑制错误信息的显示。
需要注意的是,`mysql_connect()` 函数已经在 PHP 5.5.0 版本中被废弃,建议使用 mysqli 或 PDO 替代。另外,直接使用变量拼接 SQL 语句可能存在 SQL 注入漏洞,应该使用参数化查询或者 ORM 框架来避免这个问题。
相关问题
<!DOCTYPE html> <?php require_once('MkEncrypt.php'); MkEncrypt('1234'); //这个就是密码 ?> <html> <head> <meta charset="UTF-8"> <title>GOLA 2.0</title> </head> <style type="text/css"> .wrapper {width: 1000px;margin: 20px auto;} h2 {text-align: center;} .add {margin-bottom: 20px;} .add a {text-decoration: none;color: #fff;background-color: green;padding: 6px;border-radius: 5px;} td {text-align: center;} </style> <body> <div class="wrapper"> <h2>GOLA 2.0</h2> <div class="add"> <!--<a href="adduser.html">增加新闻</a>--> </div> <table width="960" border="1"> <tr> <th>ID</th> <th>用户呀</th> <th>手机呀</th> <th>刷新密码</th> <th>总资</th> <th>可用</th> <th>操作</th> </tr> <?php // 1.导入配置文件 require "dbconfig.php"; // 2. 连接mysql $link = @mysql_connect(HOST,USER,PASS) or die("提示:数据库连接失败!"); // 选择数据库 mysql_select_db(DBNAME,$link); // 编码设置 mysql_set_charset('utf8',$link); // 3. 从DBNAME中查询到user数据库,返回数据库结果集,并按照addtime降序排列 $sql = 'select * from user order by id asc'; // 结果集 $result = mysql_query($sql,$link); // var_dump($result);die; // 解析结果集,$row为新闻所有数据,$userNum为新闻数目 $userNum=mysql_num_rows($result); for($i=0; $i<$userNum; $i++){ $row = mysql_fetch_assoc($result); echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['agent_name']}</td>"; echo "<td>{$row['phone']}</td>"; echo "<td>{$row['with_pwd']}</td>"; echo "<td>{$row['user_amt']}</td>"; echo "<td>{$row['enable_amt']}</td>"; echo "<td> <a href='javascript:del({$row['id']})'>删除</a> <a href='editnews.php?id={$row['id']}'>修改</a> </td>"; echo "</tr>"; } // 5. 释放结果集 mysql_free_result($result); mysql_close($link); ?> </table> </div> <script type="text/javascript"> function del (id) { if (confirm("确定删除这条新闻吗?")){ window.location = "action-del.php?id="+id; } } </script> </body> </html> 美化一下页面
为了美化页面,我们可以添加一些CSS样式,比如更改背景颜色、字体样式、表格边框等。可以参考下面的代码:
```
<!DOCTYPE html>
<?php
require_once('MkEncrypt.php');
MkEncrypt('1234'); //这个就是密码
?>
<html>
<head>
<meta charset="UTF-8">
<title>GOLA 2.0</title>
<style type="text/css">
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
font-size: 14px;
}
.wrapper {
width: 1000px;
margin: 20px auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0px 0px 10px #ccc;
}
h2 {
text-align: center;
color: #333;
}
.add {
margin-bottom: 20px;
text-align: right;
}
.add a {
text-decoration: none;
color: #fff;
background-color: green;
padding: 6px;
border-radius: 5px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 10px;
border: 1px solid #ccc;
}
th {
background-color: #f2f2f2;
}
td {
text-align: center;
}
td a {
text-decoration: none;
color: #333;
margin-right: 10px;
}
td a:hover {
color: red;
}
</style>
</head>
<body>
<div class="wrapper">
<h2>GOLA 2.0</h2>
<div class="add">
<!--<a href="adduser.html">增加新闻</a>-->
</div>
<table>
<tr>
<th>ID</th>
<th>用户呀</th>
<th>手机呀</th>
<th>刷新密码</th>
<th>总资</th>
<th>可用</th>
<th>操作</th>
</tr>
<?php
// 1.导入配置文件
require "dbconfig.php";
// 2. 连接mysql
$link = @mysql_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysql_select_db(DBNAME,$link);
// 编码设置
mysql_set_charset('utf8',$link);
// 3. 从DBNAME中查询到user数据库,返回数据库结果集,并按照addtime降序排列
$sql = 'select * from user order by id asc';
// 结果集
$result = mysql_query($sql,$link);
// var_dump($result);die;
// 解析结果集,$row为新闻所有数据,$userNum为新闻数目
$userNum=mysql_num_rows($result);
for($i=0; $i<$userNum; $i++){
$row = mysql_fetch_assoc($result);
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['agent_name']}</td>";
echo "<td>{$row['phone']}</td>";
echo "<td>{$row['with_pwd']}</td>";
echo "<td>{$row['user_amt']}</td>";
echo "<td>{$row['enable_amt']}</td>";
echo "<td>
<a href='javascript:del({$row['id']})'>删除</a>
<a href='editnews.php?id={$row['id']}'>修改</a>
</td>";
echo "</tr>";
}
// 5. 释放结果集
mysql_free_result($result);
mysql_close($link);
?>
</table>
</div>
<script type="text/javascript">
function del (id) {
if (confirm("确定删除这条新闻吗?")){
window.location = "action-del.php?id="+id;
}
}
</script>
</body>
</html>
```
这样,我们就可以得到一个美化后的页面。
<?php include "03_connectDB.php"; $user = $_POST["usr"]; $pwd = $_POST["pwd"]; $usertype=$_POST["usrtype"]; 跳转页面后如何获取$user和$usertype变量
要获取 `$user` 和 `$usertype` 变量,你需要将它们存储在 session 中,然后在跳转后的页面中调用 session 来获取这些变量。具体操作如下:
1. 在 PHP 文件开头调用 `session_start()` 函数开启 session。
2. 存储 `$user` 和 `$usertype` 变量到 session 中,例如:
```php
$_SESSION["user"] = $user;
$_SESSION["usertype"] = $usertype;
```
3. 在跳转后的页面中调用 `session_start()` 函数开启 session。
4. 通过 `$_SESSION` 调用存储在 session 中的变量,例如:
```php
$user = $_SESSION["user"];
$usertype = $_SESSION["usertype"];
```
注意:使用 session 存储变量时,要确保在每个需要访问这些变量的页面中都开启了 session,并且变量名字必须保持一致。