>  기사  >  백엔드 개발  >  PHP로 MySQL 데이터베이스를 운영하는 방법에 대한 자세한 설명(2)

PHP로 MySQL 데이터베이스를 운영하는 방법에 대한 자세한 설명(2)

黄舟
黄舟원래의
2017-03-25 09:45:102061검색

뉴스 목록 읽기 및 삭제

구현 코드

1. 먼저 MySQL 데이터베이스

//*******************PHP连接MySQL数据库********************
<?php
//声明PHP输出数据的字符集
header("content-type:text/html;charset=utf-8");

//0.数据库配置信息
$db_host = "localhost:3306";
$db_user = "root";
$db_pwd = "yeoman";
$db_name = "yeoman92";

//1.PHP连接MySQL服务器
$link = @mysql_connect($db_host, $db_user, $db_pwd);
if(!$link){
    echo "MySQL服务器连接失败!".mysql_error();
    exit();
}

//2.选择数据库
if(!mysql_select_db($db_name, $link)){
    echo "选择{$db_name}数据库失败!".mysql_error();
    exit();
}

//3.设置MySQL返回的数据字符集
mysql_query("set names utf8");

?>

function 연결을 위한 별도의 PHP 파일(conn.php)을 작성합니다.
header()

  • 설명: 사용자 정의 http 메시지를 보냅니다. 즉, PHP 또는 문자 집합에서 반환되는 데이터 형식입니다. .

  • 구문: void 헤더(문자열 $string)

  • 예:

    • header(“content-type:text/html;charset=utf-8”) //PHP에서 반환하는 데이터의 문자 집합 설정

    • header(“location:http :www.sina.com.cn”); //웹페이지 이동

2.메인파일(index.php)을 작성합니다

<?php

//包含连接MySQL的文件,即文件引入
include "conn.php";

//执行查询的语句
$sql = "SELECT * FROM 007_news ORDER BY id DESC";
$result = mysql_query($sql); //返回的是结果集

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>新闻列表</title>
<script type="text/javascript">function confirmDel(id){
    //询问是否删除记录
    if(window.confirm("你确定要删除吗?")){        
    //跳转到PHP的删除页面 del.php
        location.href = "del.php?id="+id;
    }
}
</script>
</head>
<body>
<table width="70%" border="1" bordercolor="#CCC" rules="all" align="center" cellpadding="5">
    <tr bgcolor="#efefef">
        <th>编号</th>
        <th>新闻标题</th>
        <th>作者</th>
        <th>来源</th>
        <th>排序</th>
        <th>点击率</th>
        <th>发布日期</th>
        <th>操作选项</th>
    </tr>
    <?php 
    $str = "";
    while($arr = mysql_fetch_array($result,MYSQL_ASSOC)){    //输出表格内容
        $str .= "<tr>\n";
        $str .= "	<td>".$arr[&#39;id&#39;]."</td>\n";
        $str .= "	<td>".$arr[&#39;title&#39;]."</td>\n";
        $str .= "   <td>".$arr[&#39;author&#39;]."</td>\n";
        $str .= "	<td>".$arr[&#39;source&#39;]."</td>\n";
        $str .= "	<td>".$arr[&#39;orderby&#39;]."</td>\n";
        $str .= "	<td>".$arr[&#39;hits&#39;]."</td>\n";
        $str .= "	<td>".date("Y-m-d  H:i", $arr[&#39;addate&#39;])."</td>\n";
        $str .= "	<td><a href=&#39;javascript:void(0)&#39;>修改</a> 
        <a href=&#39;javascript:void(0)&#39; onClick=&#39;confirmDel(".$arr[&#39;id&#39;].")&#39;>删除</a></td>\n";
        $str .= "</tr>\n";     
    }
    echo $str;
    ?></table></body></html>

구문 구조 설명

  • 1. 구문 구조 포함(함수 아님)

    • 설명 : 지정한 파일을 포함시켜 실행합니다.

    • 구문: include $filename 또는 include($filename)

    • 예: include “include/conn.php”

  • 2. 구문 구조 필요

    • 포함된 파일이 없으면 경고 오류가 보고되고 스크립트가 아래에서 계속 실행됩니다.

    • 그리고 require는 치명적인 오류를 보고하고 스크립트는 즉시 실행을 종료합니다.

    • 설명 : 지정한 파일을 포함시켜 실행합니다.

    • 구문: require $filename 또는 require($filename)

    • 예: require “include/conn.php”

    • 참고: 포함 파일과 실행 파일을 모두 포함하고 요구하지만 차이점이 있습니다.

PHP에서 파일을 가져오는 방법에 대한 자세한 내용은 다음을 참조하세요. PHP에서 파일을 가져오는 네 가지 방법과 차이점

3 . 삭제 기록 파일(del.php)을 작성하세요

<?php

//包含连接MySQL的文件,即文件引入
include "conn.php";

//执行查询的语句
$sql = "SELECT * FROM 007_news ORDER BY id DESC";
$result = mysql_query($sql); //返回的是结果集

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>新闻列表</title>

<script type="text/javascript">
function confirmDel(id){
    //询问是否删除记录
    if(window.confirm("你确定要删除吗?")){
        //跳转到PHP的删除页面 del.php
        location.href = "del.php?id="+id;
    }
}
</script>

</head>

<body>
<table width="70%" border="1" bordercolor="#CCC" rules="all" align="center" cellpadding="5">
    <tr bgcolor="#efefef">
        <th>编号</th>
        <th>新闻标题</th>
        <th>作者</th>
        <th>来源</th>
        <th>排序</th>
        <th>点击率</th>
        <th>发布日期</th>
        <th>操作选项</th>
    </tr>
    <?php 
    $str = "";
    while($arr = mysql_fetch_array($result,MYSQL_ASSOC)){    //输出表格内容
        $str .= "<tr>\n";
        $str .= "	<td>".$arr[&#39;id&#39;]."</td>\n";
        $str .= "	<td>".$arr[&#39;title&#39;]."</td>\n";
        $str .= "   <td>".$arr[&#39;author&#39;]."</td>\n";
        $str .= "	<td>".$arr[&#39;source&#39;]."</td>\n";
        $str .= "	<td>".$arr[&#39;orderby&#39;]."</td>\n";
        $str .= "	<td>".$arr[&#39;hits&#39;]."</td>\n";
        $str .= "	<td>".date("Y-m-d  H:i", $arr[&#39;addate&#39;])."</td>\n";
        $str .= "	<td><a href=&#39;javascript:void(0)&#39;>修改</a> 
        <a href=&#39;javascript:void(0)&#39; onClick=&#39;confirmDel(".$arr[&#39;id&#39;].")&#39;>删除</a></td>\n";
        $str .= "</tr>\n";     
    }
    echo $str;
    ?>
</table>
</body>
</html>

실행 효과가 표시됩니다

PHP로 MySQL 데이터베이스를 운영하는 방법에 대한 자세한 설명(2)

뉴스 기록 삭제 업무 프로세스

PHP로 MySQL 데이터베이스를 운영하는 방법에 대한 자세한 설명(2)

위 내용은 PHP로 MySQL 데이터베이스를 운영하는 방법에 대한 자세한 설명(2)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.