<?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