所以,我试图从我的数据库中获取所有行,然后能够在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语句中是否应该使用前面而不是while循环?
一旦我拥有对象中的所有行(或对象数组?idk!?!?),我该如何检索这些值?