我有档案 System.out.println(something.getClass().getName());
和index.php
。在view.php
中,有一个表单可以处理一些插入数据到MYSQL表。 index.php
的功能是获取view.php
中人员插入的数据表。我的问题是“是否有任何代码(php,javascript等)在插入数据后立即获取数据?”并且如果数据已插入index.php
,我们可以听到index.php
中播放的声音,如通知声音。
答案 0 :(得分:0)
下面是简单的代码,请参考
首先创建一个表
CREATE TABLE IF NOT EXISTS `messageTest` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`notification` varchar(255) NOT NULL,
`status` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
然后创建View.php,如
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
$('#notification_count').html(msg);
}
function playSound(filename){
document.getElementById("sound").innerHTML='<audio autoplay="autoplay"><source src="' + filename + '.mp3" type="audio/mpeg" /><source src="' + filename + '.ogg" type="audio/ogg" /><embed hidden="true" autostart="true" loop="false" src="' + filename +'.mp3" /></audio>';
}
function waitForMsg(){
$.ajax({
type: "GET",
url: "select.php",
async: true,
cache: false,
timeout:50000,
success: function(data){
if(data>0){
playSound("mymp3");
}
addmsg("new", data);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg,
15000);
}
});
};
$(document).ready(function(){
waitForMsg();
});
</script>
<span id="notification_count"></span>
<a href="#" id="notificationLink" onclick = "return getNotification()">Notifications</a>
<div id="HTMLnoti" style="textalign:center"></div>
然后选择.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabaseName";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * from messageTest where status = 'unread'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$count = $result->num_rows;
echo $count;
$conn->close();
然后是index.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabaseName";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO messageTest (id, notification, status) VALUES (1, 'New notification', 'unread')";
$result = $conn->query($sql);
$conn->close();