dougua4836 2019-04-28 23:01
浏览 45
已采纳

如何获取mysql查询并将其存储在var [duplicate]中

i am making a simple posting system for a flutter app and a requirement is it must save the post as a JSON file i have the log in done and same with the posting but i am trying to include the name of the user that posted the post by qwering for the current user and writing that to the file i have function that successfully can echo the full name of the user but i am un able to store the var in a way that lets me write it to the file

I have tried a couple of basic examples of querying and storing in var but to no success, I have also tried returning the var of the $fullname that I have in my function

this is my function to get the Fullname(this works) :

  public function get_fullname($uid){
    $sql3="SELECT fullname FROM users WHERE uid = $uid";
    $result = mysqli_query($this->db,$sql3);
    $user_data = mysqli_fetch_array($result);

    return $user_data['fullname'];

}

this is my code to write the json file (works):

$todays_date = date("y-m-d h:i:sa");
$data = array(

  'name' =>  $user_data['fullname'],
  'Title' => $Title,
  "post" => $Post,
  'date'=> $todays_date,
  'uid'=> $uid

);
$tittle = $user_data['fullname'] . "_post_on_" . $todays_date . ".json";


    function testfun($tittle, $data)
    {

    $fh = fopen("posts/$tittle", 'w')
    or die("error opening output file");
    fwrite($fh, json_encode($data,JSON_UNESCAPED_UNICODE));
    fclose($fh);
    }

    if(array_key_exists('submit',$_POST)){
       testfun($tittle, $data);
    }

the json comes out like this :

name    null
Title   "post test 1"
post    "this is a test"
date    "19-04-28 10:47:13pm"
uid "1"

but i need this :

name    "CURENT_USER"
Title   "post test 1"
post    "this is a test"
date    "19-04-28 10:47:13pm"
uid "1"
</div>
  • 写回答

1条回答 默认 最新

  • duanbu4962 2019-04-29 00:30
    关注

    Your prompt $user_data['fullname'] is empty outside of function, call your function:

    $data = array(
    
      'name' =>  get_fullname($uid),
      'Title' => $Title,
      "post" => $Post,
      'date'=> $todays_date,
      'uid'=> $uid
    
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害