如何返回从MySQL提取到一个PHP文件作为JSON数据? [英] How to return data fetched from MySQL into a php file as JSON?

查看:122
本文介绍了如何返回从MySQL提取到一个PHP文件作为JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从MySQL表中读取的数据返回到PHP文件的JSON。这是我的code连接到MySQL和安培;从它那里得到的数据。现在,我怎么能恢复它作为JSON。

 < PHP
    $用户名=用户;
    $密码=********;
    $主机=localhost的;
    $胸径=的mysql_connect($主机名,用户名$,$密码)
        或死亡(无法连接到MySQL);
    打印连接到MySQL< BR>中;
    $选定= mysql_select_db(规范,$胸径)
        或死亡(无法选择first_test);
    // $行=阵列();

    $查询=SELECT * FROM user_spec;
    $结果= mysql_query($查询);

    //则mysql_close($胸径);
    ?>
 

在满堆,我必须实现。对于第3步,我渲染动态使用用户输入尽管它没有使用任何引擎,但直接用输入值,所以我要看看它是如何做的,一旦我得到的JSON数据的列表。我把堆栈,使您的人可以亲切地看到它是我必须做的时候可能帮助我。

  1. 用户加载HTML页面
  2. 的页面,从AJAX请求并获得期权作为JSON(或者它已经存在于数据库中,否则会产生一个新的选项)
  3. 的JSON是用JS模板引擎(PURE在我们的例子)渲染
  4. 用户变化的东西
  5. 同样的JSON被修改并通过POST发送到服务器
  6. 服务器读取JSON并将其存储在数据库中(你将数据写入到文件)。然后再回到第4步,以等待其他用户的变化。
解决方案

由于只有一个 user_spec 行返回,您可以使用内置的 json_en code 功能:

 < PHP
$用户名=用户;
$密码=********;
$主机=localhost的;
$胸径=的mysql_connect($主机名,用户名$,$密码)
    或死亡(无法连接到MySQL);

//打印连接到MySQL< BR>中;

$选定= mysql_select_db(规范,$胸径)
    或死亡(无法选择first_test);

$查询=SELECT * FROM user_spec;
$结果= mysql_query($查询);

回声json_en code(mysql_fetch_assoc($结果));

?>
 

应该做的伎俩。

即使你使用的是PHP的旧版本,你可以找到一个合适的功能在 json_en code PHP手册页使用在用户评论它的地方。

I have to return data fetched from MySQL table into a php file as JSON. Here is my code to connect to mysql & get data from it. How could now I return it as JSON.

<?php
    $username = "user";
    $password = "********";
    $hostname = "localhost";    
    $dbh = mysql_connect($hostname, $username, $password) 
        or die("Unable to connect to MySQL");
    print "Connected to MySQL<br>";
    $selected = mysql_select_db("spec",$dbh) 
        or die("Could not select first_test");
    //$rows = array();  

    $query = "SELECT * FROM user_spec"; 
    $result=mysql_query($query);

    //mysql_close($dbh);
    ?>

Under is full stack that I have to implement. For step 3, I am rendering the list dynamically using user inputs though its not using any engine but directly using input values so I have to see it how to do once I get JSON data. I put the stack so that you people can kindly see it what I have to do when possibly helping me.

  1. the user load an HTML page
  2. the page make an ajax call and get the options as a JSON(either it exists already in the database, or a new option set is generated)
  3. the json is rendered using a JS templating engine (PURE in our case)
  4. the user change something
  5. the same JSON is modified and sent by a POST to the server
  6. the server read that JSON and store it in the database(you would write the data to your file). And then go back to the step 4, to wait for another user change.

解决方案

Given that only one user_spec row is returned you can use the built in json_encode function:

<?php
$username = "user";
$password = "********";
$hostname = "localhost";    
$dbh = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL");

//print "Connected to MySQL<br>";

$selected = mysql_select_db("spec",$dbh) 
    or die("Could not select first_test");

$query = "SELECT * FROM user_spec"; 
$result=mysql_query($query);

echo json_encode(mysql_fetch_assoc($result));

?>

Should do the trick.

Even if you are using an older version of PHP, you can find a suitable function in the user comments at the json_encode PHP Manual page to use in it's place.

这篇关于如何返回从MySQL提取到一个PHP文件作为JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆