使用JSON调用php数组后获取

我正在尝试获取一个数组,即json_encoded. 该数组还包含字典。举个例子,它看起来像这样[{key:'value'}]有 5 个键,每一行都有相同的键但不同的值。字典中有大约 70000 个项目,因为它们是从文件中读取并放入 for while 循环以将它们放入数组中。


while($row = $result->fetch_assoc())

{

    $tmp[] = $row;

}

$test = json_encode($tmp);

数组和字典是从MySql表中提取的,键与列相关联。我的文件被命名ipv4.php,jsIPv4.php 因此数组和 dicti 被定义并放入一个变量中,ipv4并被调用到一个javascript代码中jsIPv4,我尝试将它存储到一个变量中并使用JSON.parse. 但在此之前,我至少需要打印出数组中的值。


这就是我如何称呼我的php变量。


<script type="text/javascript">

    var test = <?php echo $test ?>;

    document.write(test);

</script>

如果我把它放在 '' 中,它会列出我需要的所有东西,但是当我尝试将它称为索引时:test[0]我将[作为 1. 值出来,所以我猜它会转换成一个字符串。


回首忆惘然
浏览 99回答 2
2回答

慕尼黑5688855

通过 jquery ajax 传递它会很棒。我根据您的要求为您重新编写了一个很好的插图。假设您的表具有列id、用户名、名称、电子邮件等......或者您可以按如下方式创建表并插入其中进行测试create table users_test(id int primary key auto_increment,username varchar(30),name varchar(30), email varchar(30));插入测试insert into users_test(username,name,email) values('nancy','nancy moore','nany@gmail.com');insert into users_test(username,name,email) values('tony','tony moore','tony@gmail.com');索引.html<!doctype html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp;<script&nbsp;&nbsp; src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">&nbsp;&nbsp; type="text/javascript" charset="utf-8"></script>&nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript">$(document).ready(function(){&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: 'test.php',&nbsp; &nbsp; &nbsp; &nbsp; type: 'get',&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'JSON',&nbsp; &nbsp; &nbsp; &nbsp; success: function(response){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var len = response.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i=0; i<len; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var id = response[i].id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var username = response[i].username;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var name = response[i].name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var email = response[i].email;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var tr_str = "<tr>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<td align='center'>" + (i+1) + "</td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<td align='center'>" + username + "</td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<td align='center'>" + name + "</td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<td align='center'>" + email + "</td>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#userTable tbody").append(tr_str);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});</script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table id="userTable" border="1" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="5%">S.no</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="20%">Username</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="20%">Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="30%">Email</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody></tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body></html>测试文件<?php$host = "localhost"; /* Host name */$user = "root"; /* User */$password = ""; /* Password */$dbname = "you db here"; /* Database name */$con = mysqli_connect($host, $user, $password,$dbname);// Check connectionif (!$con) {&nbsp;die("Connection to db failed ");}$test = array();$query = "SELECT * FROM users_test ORDER BY id";$result = mysqli_query($con,$query);while($row = mysqli_fetch_array($result)){&nbsp; &nbsp; $id = $row['id'];&nbsp; &nbsp; $username = $row['username'];&nbsp; &nbsp; $name = $row['name'];&nbsp; &nbsp; $email = $row['email'];&nbsp; &nbsp; $test[] = array("id" => $id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "username" => $username,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name" => $name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "email" => $email);}// Encoding array in JSON formatecho json_encode($test);

明月笑刀无情

好吧,我尝试了此代码并使用了价值<?phpwhile($row = $result->fetch_assoc()){&nbsp; &nbsp; $tmp[] = $row;}&nbsp;$test = json_encode($tmp);?><script type="text/javascript">&nbsp; &nbsp; var test = <?php echo $test; ?>;&nbsp; &nbsp; document.write(JSON.stringify(test));</script>&nbsp;My data output : [{"id":"1","name":"ranbir","city":"delhi","age":"35"},{"id":"2","name":"sonu","city":"punjab","age":"40"},{"id":"3","name":"ranbir","city":"delhi","age":"35"},{"id":"4","name":"sonu","city":"punjab","age":"40"}]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript