


A simple example of outputting database query results in json format in PHP. The query result json
include/conn.php is a database link file, which cannot be searched online
<?php include './include/conn.php'; //数据库链接文件 $sql_notice = mysql_query('SELECT * FROM gg_notice where enable = "1" limit 0,10'); $notice = mysql_fetch_array($sql_notice, MYSQL_ASSOC); print_r ($notice); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>第一php网提供的教程--将数据库读取的数据生成json格式</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"/></script> --> <script language=javascript> </script> </head> <body> <pre class="brush:php;toolbar:false"> <h1 id="请注意两种方法生成的对象数组在结构上的区别">请注意两种方法生成的对象数组在结构上的区别</h1> <?php echo '<h1 id="法一">法一</h1>'; //假设以下数组是根据我们从数据库读取的数据生成的 $jarr=array('total'=>239,'row'=>array( array('code'=>'001','name'=>'中国','addr'=>'Address 11','col4'=>'col4 data'), array('code'=>'002','name'=>'Name 2','addr'=>'Address 12','col4'=>'col4 data'), ) ); //法一: $jobj=new stdclass();//实例化stdclass,这是php内置的空类,可以用来传递数据,由于json_decode后的数据是以对象数组的形式存放的, //所以我们生成的时候也要把数据存储在对象中 foreach($jarr as $key=>$value){ $jobj->$key=$value; } print_r($jobj);//打印传递属性后的对象 echo '使用$jobj->row[0][\'code\']输出数组元素:'.$jobj->row[0]['code'].'<br>'; echo '编码后的json字符串:'.json_encode($jobj).'<br>';//打印编码后的json字符串 echo '<hr>'; //法二: echo '<h1 id="法二">法二</h1>'; echo '编码后的json字符串:'; echo $str=json_encode($jarr);//将数组进行json编码 echo '<br>'; $arr=json_decode($str);//再进行json解码 print_r($arr);//打印解码后的数组,数据存储在对象数组中 echo '使用$arr->row[0]->code输出数组元素:'.$arr->row[0]->code; ?> </body> </html>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool