PHP +ajax +sql for data interaction

Source: Internet
Author: User
Tags php server
1. First create a new SQL table with the table contents as shown above:


2. Create a new public file conn.php to link to the database:


  
   Header ("Content-type:text/html;charset=utf8");//Declaration Encoding format $conn =mysql_connect ("localhost"," Root "," aaaaaa "ordie (" database connection error ". Mysql_errno ());//Connect SQL Mysql_ select_db ("Phptest", $conn); mysql_query (' Set NAMES UTF8 'ordie (' CharSet set error '?> 
The 3.php server is provided to the front-end Ajax data interface, new file Phptoajax. Php

  
     require_once ("conn.php");//import Common file $query =mysql_query ("select * from TXT"ordie (" error prompt:  ". Mysql_error ()); $jsonArray =arraywhile ($rows =mysql_fetch_array ($query)) {
Handles automatic content within the database  $rows [' content ']=mb_substr (Strip_tags (Htmlspecialchars_decode ($rows [' Content '])), 0,100,"Utf-8");  Add the contents of the database to the new array  
Array_push ($jsonArray, $rows);//note here is $rows  json_encode ($jsonArray);//Convert to JSON to pass to the front end
4. New phptoajax.html front end receive data, here I use jquery encapsulated Ajax method, after the execution of the page as shown:
HTML><HTML><Headlang="en"> <Metacharset="UTF-8"> <title>
    Title> <Scripttype="Text/javascript"src="Jquery-1.8.3.min.js">
    Script>
    Head><Body><ulid="List">
  
 
      
Ul><Scripttype="Text/javascript"> $ (function() {$.ajax ({type:"POST",//Transfer methodURL:"phptoajax.php",//Data interfaceDataType:"JSON",//Receive format success:function(msg)//If received successfully execute the following {varLi=""; for(varI =0;i <> length-1;i++)//Here is a limit of 10 {li+= "
  • "+msg[i][' title ']+"

    "+msg[i][' content ']+" ... +msg[i][' id ']+"' target= ' _blank ' > details

  • "; } $ ("#list"). html (LI); }, Error:function()//If receive does not successfully perform the following
                {                    console.log (" link error ")});}    );
      
           Script>
     
          body>
     
          html>
    5. Click the "detailed" link shown in the previous step to view the corresponding article content and create a new phpartcle.php file
    
      
          require_once ("conn.php"); $id =$_get[' art '];//receive front-end uploaded data
    Querying the database for the corresponding content
    $query =mysql_query ("select * FROM txt WHERE  id= ' $id '") or Die ("article Error:". Mysql_error ());
    Iterating through an array, displaying content
    if ($rows =mysql_fetch_array ($query)) { "

    ". $rows [' title ']. "

    "; " ". Htmlspecialchars_decode ($rows [' content ']). "";}

    -------------------finished-----------------------


  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.