此代碼可以100%工作,但是當我嘗試使用JSON
時,它不顯示任何數據。json不能與pdo一起工作,但可以與mysql_函數一起使用
的問題是,我想改變我的所有代碼PDO
,因爲有人告訴我,mysql_*
函數的計算折舊。
<?php
$con = new PDO('mysql:host=localhost;dbname=adnan;charset=UTF-8','root','');
$sql= $con->query('select * from adnan_user');
while($row =$sql->fetch()){
echo $row['user_id'],"\n";
}
?>
下面是我在json
調用代碼:當我使用mysql_
功能 但隨着pdo
它不工作了也有效。
<?php
$con = new PDO('mysql:host=localhost;dbname=adnan;charset=UTF-8','root','');
$sql= $con->query('select * from adnan_user');
while($row =$sql->fetch()){
$name = $row['name'];
$user= $row['user'];
}
// The JSON standard MIME header.
header('Content-type: application/json');
$array = array('name'=>$name, 'user'=>$user);
echo json_encode($array);
?>
對JSON的代碼
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$.getJSON('json.php', function(data) {
$('#myJson').html('<table style="color:red"><tr><td>' + data.name + '</td><td>' + data.user + '</td></tr></table>');
});
});
</script>
</head>
<body>
<div id="myJson"></div>
</body>
</html>
你是什麼意思「它不工作」。是否有錯誤消息,它是無效的JSON或沒有發生任何事情? –
當我使用mysql_時,一切都沒有發生,它確實顯示了目標結果 – Musa
@LouisH。不,在PHP循環塊中沒有自己的作用域 –