在javascript对象中应用php/MySql while循环 [英] Apply php/MySql while loop in javascript object

查看:66
本文介绍了在javascript对象中应用php/MySql while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行以下php/mysql循环,并将结果:$ test,$ test1,$ test2应用于javascript代码中的var Data对象.这将使var Data动态,从数据库中提取其数据以构造对象.

I would like to take the following php/mysql loop, and apply the results: $test,$test1,$test2 to the var Data object in the javascript code. This will make the var Data dynamic, pulling its data to construct the object form the database.

推荐答案

<?php
include("regDBConnect.php");

// collect all the results
$rows = array();

$result1 = mysql_query("SELECT * FROM Phase where Pid = 1", $db) or die("cannot select");
while($row = mysql_fetch_array($result1)) {
  $rows []= array(
    'id' => $row['id'],
    'parent' => $row['parent'],
    'name' => $row['name'],
  );

  /*
     if you remove the line above and uncomment this instead,
     javascript objects will see all the properties you selected from the DB 
   */
  // $rows []= $row;
}
?>

<script type="text/javascript">
// now output the collected results
var treeData = <?php echo json_encode($rows); ?>;
</script>

请注意,我所说的关于PDO/MySQLi的内容仍然适用,这只是回答此特定问题的一个简单示例. (通常,您应该仅选择需要的列,而不要选择*.)

Note that what I said about PDO/MySQLi still applies, this is just a minimal example to answer this specific question. (And in general, you should SELECT only those columns you will need, not *.)

这篇关于在javascript对象中应用php/MySql while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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