简体   繁体   English

MySQL,PHP,JSON和JavaScript为NULL

[英]NULL with MySQL, PHP, JSON and JavaScript

I often have some JavaScript which receives data from a server's database and presents it on the page. 我经常有一些JavaScript,它们从服务器的数据库接收数据并将其显示在页面上。

$.getJSON( 'ajax.php',{id:$('bla').val()}, function(json) {
    $('#myClone').clone().removeAttr('id').text('json.a').appendTo(whatever)
});

ajax.php ajax.php

$sql='SELECT a,b,c FROM t WHERE id=?';
$stmt = $conn->prepare($sql);
$data=array($_GET['id']);
$stmt->execute($data);
header('Content-Type: application/json;');
echo(json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)));

A problem arises when the database contains NULL for a given column, and the associated JSON includes null without quotes, HTTP transmits the JSON string still with null without quotes, but JavaScript/jQuery appears to convert it into a string "null" , and null is displayed on the page instead of the desired empty string. 当数据库的给定列包含NULL且关联的JSON包含不带引号的null ,HTTP会传输仍带null且不带引号的JSON字符串,但是JavaScript / jQuery似乎将其转换为字符串"null"null在页面上显示,而不是所需的空字符串。

How is this best rectified? 如何最好地纠正? Should be be done so server-side or client-side? 应该在服务器端还是客户端这样做? If server-side, should it be done at the database level using something like SELECT IFNULL(a,"") , or the PHP application level? 如果是服务器端,是否应该使用SELECT IFNULL(a,"")或PHP应用程序级别在数据库级别完成?

Doing it at the database level will be the best. 最好在数据库级别执行此操作。 The only reason not to do it at the database level would be if you can foresee any situation in which another user of that particular database query or a higher level would need to distinguish between an empty string "" and null. 在数据库级别不这样做的唯一原因是,如果可以预见到该特定数据库查询或更高级别的另一个用户需要区分空字符串“”和null的任何情况。

Doing the query at the JavaScript display level would potentially be a bad idea because there is the possibility to forget to add this check or code in a particular output location, and null would be displayed there. 在JavaScript显示级别进行查询可能不是一个好主意,因为有可能忘记在特定的输出位置添加此检查或代码,并且在那里会显示null。 In addition, extra bytes are transmitted at every stage from the database to display, and the end user's CPU is processing the request, rather than your really fast MySQL, which will affect load time. 另外,额外的字节会在每个阶段从数据库传输到显示,最终用户的CPU正在处理请求,而不是真正快的MySQL,这会影响加载时间。

Consider a scenario with 1 million entries. 考虑一个具有100万个条目的方案。 You will do 1 million checks in MySQL (a language optimized for efficiency) instead of 1 million transmissions of 'null' from the database to PHP (an extra 2 million bytes), 1 million transmissions of 'null' from PHP to the page (another extra 2 million bytes), and 1 million checks at the client side in JavaScript (a language not known for efficiency). 您将在MySQL中进行一百万次检查(一种针对效率进行了优化的语言),而不是从数据库到PHP的一百万次“空”传输(额外的200万字节),从PHP到页面的一百万次“空”传输(另外200万字节),以及使用JavaScript(一种效率不高的语言)在客户端进行100万次检查。

Use a conditional expression: 使用条件表达式:

$.getJSON( 'ajax.php',{id:$('bla').val()}, function(json) {
    $('#myClone').clone().removeAttr('id').text(json.a === null ? '' : json.a).appendTo(whatever)
});

But I would probably go with the server-side solution you mentioned in the question. 但是我可能会使用您在问题中提到的服务器端解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM