Php + ajax + SQL for data interaction

Source: Internet
Author: User
Php + ajax + SQL for data interaction 1. create a new SQL table. The table content is as follows:


2. create a public file conn. php to connect to the database:


  Header ("Content-Type: text/html; charset = utf8"); // Declare the encoding format $ conn = mysql_connect ("Localhost","Root","Aaaaaa")Or die("Database connection error". Mysql_errno (); // Connect to SQL mysql_select_db ("Phptest", $ Conn); mysql_query ('Set NAMES utf8')Or die('Character Set setting error'. Mysql_error (); // sets the input character set encoding.?>
3. the php Server provides the front-end ajax data interface to create the phptoAJAX file. Php

     Require_once("Conn. php"); // Import a public file $ query = mysql_query ("SElECT * FROM txt")Or die("Error message:". Mysql_error (); $ jsonArray =Array(); // New data is used to receive data groups corresponding to each row of the databaseWhile($ Rows = mysql_fetch_array ($ query )){
// Process automatically corresponding content in the database $ rows ['Content'] = Mb_substr (strip_tags (htmlspecialchars_decode ($ rows ['Content']), 0,100,"UTF-8"); // Add the database content to the new array
Array_push ($ jsonArray, $ rows); // note that $ rows}EchoJson_encode ($ jsonArray); // Convert to json and pass it to the front end
4. create a new phpToAJAX. HTML front-end to receive data. here I use the ajax method encapsulated by jquery, as shown in the page after execution:
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", // Receiving format success:Function(Msg) // if the receiving succeeds, execute the following {VarLi ="";For(VarI = 0; I Length-1; I ++) // 10 {li + = "
  • "+ Msg [I] ['Title'] +"

    "+ Msg [I] ['Content'] +"... + Msg [I] ['Id'] +"'Target = '_ blank'>Details

  • ";} $ ("# List").Html (li) ;}, error:Function() // If the receiving fails, execute the following
    {Console. log ("Link error")}});});
         Script>
        Body>
        Html>
    5. click the "details" link shown in the previous step to view the corresponding article content and create a phpArtcle. php file.
    
          Require_once("Conn. php"); $ Id = $ _ GET ['Art']; // Receives data uploaded by the front-end
    // Query the corresponding content of the database
    $ Query = mysql_query ("SELECT * FROM txt WHERE id = '$ ID'") or die ("article error:". mysql_error ());
    // Traverse the array to display the content
    If($ Rows = mysql_fetch_array ($ query )){Echo"". $ Rows ['Title']."";Echo"

    ". Htmlspecialchars_decode ($ rows ['Content'])."

    ";}

    ------------------- Finished -----------------------


    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.