我有这个数据库,我对它执行左连接,我有Null值,我想删除这些空值。我是附加我的数据库&显示查询的PHP脚本。
如果有人可以帮助我从JSON中删除Null值吗? 我想根据感兴趣的团队表检索用户感兴趣的所有新闻。
teaminterest表
teaminterests_id |userName |teamName
-------------------------------------------------
1 | Tomas | Real Madrid
--------------------------------------------------
2 | Tomas | FCB. Barcelona
--------------------------------------------------
3 | Carl | Real Madrid
teamnews表
teamnews_id |teamnews_title |teamnews_image|teamnews_text|teamnews_timeStamp |teamnews_newsURL |TeamName
----------------------------------------------------------------------------------------------------------------------
3 | Barcelona News 1 | XYZ.jpg | Dummy Text |2016-04-23 17:51:23| Dummy Website.com | FCB. Barcelona
----------------------------------------------------------------------------------------------------------------------
4 | RealMadrid News1 | ZYX.jpg | Dummy Text |2016-04-23 17:51:23| Dummy Website.com | Real Madrid
----------------------------------------------------------------------------------------------------------------------
5 | RealMadrid News2 | ZYX.jpg | Dummy Text |2016-04-23 17:51:23| Dummy Website.com | Real Madrid
----------------------------------------------------------------------------------------------------------------------
6 | RealMadrid News3 | ZYX.jpg | Dummy Text |2016-04-23 17:51:23| Dummy Website.com | Real Madrid
----------------------------------------------------------------------------------------------------------------------
7 | Barcelona News 2 | XYZ.jpg | Dummy Text |2016-04-23 17:51:23| Dummy Website.com | FCB. Barcelona
如果我将此URL发送到此PHP SCRIPT
http://localhost/HiFootball/NewsFragment/test.php?userName=tomas
PHP SCRIPT
<?php
require('C:\wamp\www\HiFootball\config.php');
$conn = mysqli_connect($servername, $username, $password, $db);
$query="
select distinct *
from teaminterest
left join teamnews
on teaminterest.userName='".$_GET['userName']."' and teamnews.TeamName=teaminterest.teamName
order by teamnews_timeStamp desc
";
$result = mysqli_query($conn,$query);
$rows = array();
echo mysqli_error($conn);
while($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
echo json_encode($rows);
?>
我得到这个JSON表格:
[
{"teaminterests_id":"104","userName":"Tomas","teamName":"Real Madrid","teamnews_id":"4","teamnews_title":"Real Madrid News 1 ","teamnews_image":"ZYX.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:52:23","teamnews_newsURL":"www.DummWebsite.com","TeamName":"Real Madrid"}
,
{"teaminterests_id":"104","userName":"Tomas","teamName":"Real Madrid","teamnews_id":"6","teamnews_title":"Real Madrid News 2\r\n","teamnews_image":"ZYX.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:52:23","teamnews_newsURL":"www.DummWebsite.com","TeamName":"Real Madrid"}
,
{"teaminterests_id":"104","userName":"Tomas","teamName":"Real Madrid","teamnews_id":"7","teamnews_title":"Real Madrid News 3","teamnews_image":"ZYX.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:52:23","teamnews_newsURL":"www.DummWebsite.com","TeamName":"Real Madrid"}
,{"teaminterests_id":"103","userName":"Tomas","teamName":"Barca","teamnews_id":"3","teamnews_title":"Barcelona News 1 ","teamnews_image":"XYZ.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:51:23","teamnews_newsURL":"WWW.dummywebsite.com","TeamName":"Barca"}
,{"teaminterests_id":"103","userName":"Tomas","teamName":"Barca","teamnews_id":"8","teamnews_title":"Barcelona News 2","teamnews_image":"XYZ.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:51:23","teamnews_newsURL":"WWW.dummywebsite.com","TeamName":"Barca"}
,
{"teaminterests_id":"105","userName":"Carl","teamName":"Real Madrid","teamnews_id":null,"teamnews_title":null,"teamnews_image":null,"teamnews_text":null,"teamnews_timeStamp":null,"teamnews_newsURL":null,"TeamName":null}
]
答案 0 :(得分:0)
从ON
tho WHERE
移动部件,请使用正确的SQL查询转义。
$query="select distinct *
from teaminterest
left join teamnews
on teamnews.TeamName=teaminterest.teamName
where teaminterest.userName='".mysqli_real_escape_string($conn, $_GET['userName'])."'
order by teamnews_timeStamp desc
";