因此,我试图从数据库中取出所有行,然后能够在 javascript 中列出和操作它们。到目前为止,我所有的研究都指向了物体。
到目前为止,这是我的代码:
<?php
if($_SESSION['username']) {
$sql = "SELECT * FROM potentials";
$result = mysql_query($sql) or die(mysql_error());
$potential = mysql_fetch_array($result);
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
$myJSON = json_encode($array);
echo $myJSON;
?>
还有我的 javascript:
<script type="text/javascript">
$(document).ready(function(){
var potentialModels = <?php print($myJSON); ?>;
console.log(potentialModels);
});
</script>
我在控制台中返回了这个:
[Object]
0: Object
length: 1
__proto__: Array[0]
但不应该是:
Object {Models: Array[2]}
Models: Array[2]
0: Object
address: "407 Forby Estates Dr "
age: ""
city: "Eureka"
dayofweek: ""
email: "rebekahkeller@yahoo.com"
firstname: "Maddie"
id: "54"
joindate: "2013-04-09 16:25:38"
lastname: "Keller"
parentname: "Rick Bross"
parentnumber: "0"
phone: "6367511428"
state: "MO"
timeofday: "after 9:30 am"
twitter: ""
zip: "63025"
__proto__: Object
1: Object
length: 2
__proto__: Array[0]
__proto__: Object
我应该在我的 PHP 语句中使用 aforeach 而不是 while 循环吗?
一旦我拥有对象中的所有行(或对象数组?idk!?!?),如何检索值?