PHP gets data from the database and turns it into JSON data

Source: Internet
Author: User

<?PHP//SQL statements to be executed// single    $sql= "SELECT * FROM Xinwen"; //multiple data//$sql = "Select Id,name from Tbl_user"; Invoking the conn.php file for database operations@require("conn.php"); //Prompt Operation success Information, note: $result exists in the conn.php file, is called out    if($result)       {             //$array =mysql_fetch_array ($result, MYSQL_ASSOC); Data Set             $users=Array(); $i=0;  while($row=Mysql_fetch_array($result,Mysql_assoc)) {                      $row[' Code ']. ' -----------‘.$row[' Tupian ']. ' </br> '; $users[$i]=$row; $i++; }             EchoJson_encode (Array($users)); /*Single data*/               /*$row =mysql_fetch_row ($result, MYSQL_ASSOC);  echo json_encode (Array (' jsonobj ' = $row)); */    }             Mysql_free_result($result); //Release Results    Mysql_close(); //Close Connection?>
<?PHP//Setting Database Variables    $db _host= ' localhost ';//database host name, usually localhost    $db _user= ' root ';//database user account, depending on individual circumstances    $db _PASSW= ";//database user password, depending on individual circumstances    $db _name= ' Qichezaixian ';//database specific name, whichever database you just created is a//connection database    $conn=mysql_connect($db _host,$db _user,$db _PASSW) or die(' Database connection failed! </br> error Reason: '.Mysql_error()); //set character sets, such as UTF8 and GBK, depending on the character set of the database    mysql_query("Set names ' UTF8 '"); //Selected Database    mysql_select_db($db _name,$conn) or die(' Database selection failed! </br> error Reason: '.Mysql_error()); //Execute SQL statement (query)    $result=mysql_query($sql) or die(' Database query failed! </br> error Reason: '.Mysql_error()); //Description: This code itself does not work, because it is often used, so just to reduce the workload do not have to write it repeatedly, so put it in a file, so it can be called at any time.  You just have to change the "Set database variables" and "Set up character Sets" section on a personal basis. ?>
conn.php

PHP gets data from the database and turns it into JSON data

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.