php +ajax +sql 实现数据交互

php中文网
发布: 2016-06-23 13:36:24
原创
1001人浏览过

立即学习PHP免费学习笔记(深入)”;

腾讯智影-AI数字人
腾讯智影-AI数字人

基于AI数字人能力,实现7*24小时AI数字人直播带货,低成本实现直播业务快速增增,全天智能在线直播

腾讯智影-AI数字人 73
查看详情 腾讯智影-AI数字人

1.首先新建个sql表,表内容如上所示:


2.新建个公用文件conn.php来链接数据库:

立即学习PHP免费学习笔记(深入)”;

<strong><?php</strong> header(<strong>"Content-Type:text/html;charset=utf8"</strong>);//申明编码格式 $conn=mysql_connect(<strong>"localhost"</strong>,<strong>"root"</strong>,<strong>"aaaaaa"</strong>) <strong>or die</strong>(<strong>"</strong><strong>数据库连接错误</strong><strong>"</strong>.mysql_errno());//连接sql mysql_select_db(<strong>"phptest"</strong>,$conn); mysql_query(<strong>'SET NAMES UTF8'</strong>) <strong>or die</strong>(<strong>'</strong><strong>字符集设置错误</strong><strong>'</strong>.mysql_error());//设置输入的字符集编码 <strong>?></strong>
登录后复制
<strong>3.php服务端提供给前端ajax数据接口,新建文件phptoAJAX。php</strong>
登录后复制
<pre class="n"><strong><?php</strong> <strong>require_once</strong>(<strong>"conn.php"</strong>);//导入公用文件 $query=mysql_query(<strong>"SElECT * FROM txt"</strong>) <strong>or die</strong>(<strong>"</strong><strong>错误提示:</strong><strong>"</strong>.mysql_error()); $jsonArray=<strong>array</strong>();//新建数据用于接收数据库每行对应的数据组 <strong>while</strong>($rows=mysql_fetch_array($query)) {
登录后复制
 //处理数据库里面的自动对应的内容  $rows[<strong>'content'</strong>]=mb_substr(strip_tags(htmlspecialchars_decode($rows[<strong>'content'</strong>])),0,100,<strong>"utf-8"</strong>);  //把数据库的内容添加到新建数组中  
登录后复制
 array_push($jsonArray,$rows);//注意这里是$rows  } <strong>echo </strong>json_encode($jsonArray);//转换成json传递给前端
登录后复制
4.新建phpToAJAX.HTML前端接收数据,这里我用jquery封装好的ajax方法,执行后的页面如下图所示:
登录后复制
<pre class="n"><!DOCTYPE <strong>html</strong>><<strong>html</strong>><<strong>head </strong><strong>lang=</strong><strong>"en"</strong>>    <<strong>meta </strong><strong>charset=</strong><strong>"UTF-8"</strong>>    <<strong>title</strong>></<strong>title</strong>>    <<strong>script </strong><strong>type=</strong><strong>"text/javascript" </strong><strong>src=</strong><strong>"jquery-1.8.3.min.js"</strong>></<strong>script</strong>></<strong>head</strong>><<strong>body</strong>><<strong>ul </strong><strong>id=</strong><strong>"list"</strong>><pre class="n">  <!--数据将在这里显示-->
登录后复制
ul>script type="text/javascript"> $(function(){ $.ajax({ type: "post",//传递方法 url: "phpToAJAX.php",//数据接口 dataType: "json",//接收格式 success: function(msg)//如果接收成功执行以下 { var li=""; for(var i =0;ilength-1;i++)//这里是限定10个 { li+="
  • "

  • +msg[i]['title']+"

    "

    +msg[i]['content']+"...]+"' target='_blank'>详细"; } $("#list").html(li); }, error:function()//如果接收不成功执行以下
                {                    console.log(<strong>"</strong><strong>链接错误</strong><strong>"</strong>)            }        });    });</<strong>script</strong>></<strong>body</strong>></<strong>html</strong>>
    登录后复制
    5.点击上一步图中所示的“详细”链接,可查看对应的文章内容,新建phpArtcle.php文件
    登录后复制
    <strong><?php</strong> <strong>require_once</strong>(<strong>"conn.php"</strong>); $id=$_GET[<strong>'art'</strong>];//接收前端上传的数据
    登录后复制
    <pre class="n">//查询数据库对应的内容
    登录后复制
    $query=mysql_query("SELECT * FROM txt WHERE  id='$id'") or die("文章错误:".mysql_error());
    登录后复制
    //遍历数组,显示内容
    登录后复制
    if($rows=mysql_fetch_array($query)){ echo "

    "

    .$rows['title'].""; echo "
    ".htmlspecialchars_decode($rows['content'])."
    "
    ;}

    ------------------- 完毕 -----------------------



    立即学习PHP免费学习笔记(深入)”;


    PHP速学教程(入门到精通)
    PHP速学教程(入门到精通)

    PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号