douchongbc72264 2016-07-14 18:33
浏览 33
已采纳

JSON [“数据”,“预数据库”]

So, I put my php page to connect to my android app on a hosted server (Hostgator). Now my PHP script for the JSON data seems to not be returning properly.This was working on my wamp server just fine. Example below of issue...

["data","pre database"][{"email":"thomas@wiregrass.edu","password":"test","fname":"Thomas","lname":"Cummings","phone":"5052030822","temppass":"15151","alert":"B"}]

Any ides as to what I did wrong or what is going on would be appreciated.

PHP script (might be outdated, this project is old):

<?php

$user = "ab73953_test";
$pass = "H3@ther78";
$db = "ab73953_testdb";
$out = array('data', 'pre database');
echo json_encode($out);
$db =  mysqli_connect('localhost', $user, $pass, $db) or die("did not work");


$email=$_POST['username'];
$email = "thomas@wiregrass.edu"; // testing 

$qry = 'SELECT * FROM users WHERE email = "'. $email .'"' ;

$result = mysqli_query($db, $qry) or die(" did not query");

$count = mysqli_num_rows($result);
$output = array();
if($count > 0){
 while($row = mysqli_fetch_assoc($result))
 {
   $output[]=$row;

 }

 echo json_encode($output);

}
else 
echo json_encode("Could not find user");

mysqli_close($db);
?>
  • 写回答

1条回答 默认 最新

  • douli6605 2016-07-14 18:39
    关注

    That's not valid JSON. JSON is basically javascript: If the json you generate would be a javascript syntax error, then it's not valid json.

    You have two separate echo json_encode(...) blocks, so you're producing two entirely separate/distinct json strings. Your output can only be one SINGLE json string.

    e.g. [...][...] is two separate arrays that have gotten glued together. It's a javascript syntax error, therefore it's also invalid json. If you had something like

    $arr1 = array(...);
    $arr2 = array(...);
    echo json_encode(array($arr1, $arr2));
    

    you'd end up with

    [[...],[...]]
    

    and be ok

    But you have

    echo json_encode($arr1);
    echo json_encode($arr2);
    

    and end up with

    [...][...]
    

    which is an outright syntax error.

    And note that you're vulnerable to sql injection attacks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿