php if条件判断数组返回[false]
{"code":200,"message":"success","data":[false]}
$sql_notice=mysql_query(sql语句) $arrData =array(); while ($arrData[] = mysql_fetch_array($sql_notice)) { $arrData; } $sData = array_values($arrData); if(。。。){ Response::json(200,'success',$sData); }else{ Response::json(400,'error',""); }
请注意 是[false] 不是false
因为前面应需要加了[]
这个是做的转json返回数据的接口 while循环那里若有优化的写法也请指教
现在就是因为这个 若不符合返回的会是[false] 试着用和false一样的方法去判断结果都不行
求教这里要怎么写
回复讨论(解决方案)
1、你需要得到什么?哪里出了问题?
2、Response::json 方法是怎么写的
3、while ($arrData[] = mysql_fetch_array($sql_notice)) 将会使 $arrData 数组的最后一个元素为 false
1、你需要得到什么?哪里出了问题?
2、Response::json 方法是怎么写的
3、while ($arrData[] = mysql_fetch_array($sql_notice)) 将会使 $arrData 数组的最后一个元素为 false
做一个APP接口返回json数据 数据库读出多条数据 也就是多个数组 遍历 转为json 返回给app端
现在请求验证通过的话数据都可以读出并以json呈现 但当请求没有通过验证致使读取的数据为空时,返回仍是200 成功
验证条件就是读取数据库的条件 不符合就读不到 然后判断数组为空就应该400 但是由于这个[false]不被视为空,所以结果会返回
200 成功 [false] 这样就不行了 应该这种情况下是400失败的
1、你需要得到什么?哪里出了问题?
2、Response::json 方法是怎么写的
3、while ($arrData[] = mysql_fetch_array($sql_notice)) 将会使 $arrData 数组的最后一个元素为 false
class Response { const JSON = "json"; /** * * @param integer $code 状态码 * @param string $message 提示信息 * @param array $data 数据 * @param string $type 数据类型 * return string */ public static function show($code, $message = '', $data = array(), $type = self::JSON) { if(!is_numeric($code)) { return ''; } $type = isset($_GET['format']) ? $_GET['format'] : self::JSON; $result = array( 'code' => $code, 'message' => $message, 'data' => $data, ); if($type == 'json') { self::json($code, $message, $data); exit; }else{ // TODO } } /** * 按json方式输出通信数据 * @param integer $code 状态码 * @param string $message 提示信息 * @param array $data 数据 * return string */ public static function json($code, $message = '', $data = array()) { if(!is_numeric($code)) { return ''; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data ); echo json_encode($result); exit; }}
如果是while那句的写法有问题导致的话 求教怎么改
$arrData =array(); while ($$row = mysql_fetch_array($sql_notice)) { $arrData[] = $row; } if($arrData) { Response::json(200,'success',$arrData); } .....
我不关心你的目的是什么,只关心你需要什么样的格式
$arrData =array(); while ($$row = mysql_fetch_array($sql_notice)) { $arrData[] = $row; } if($arrData) { Response::json(200,'success',$arrData); } .....
我不关心你的目的是什么,只关心你需要什么样的格式
按我当前的模式 验证条件即是查询条件的话 判断自然是是否有数据了
我现在需要的是有就用json格式输出 返回200 成功 数据 如果没有 就返回 400 失败 ""
$arrData =array(); while ($$row = mysql_fetch_array($sql_notice)) { $arrData[] = $row; } if($arrData) { Response::json(200,'success',$arrData); } .....
我不关心你的目的是什么,只关心你需要什么样的格式
受教了 感谢版主大大

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
