は、以下の簡単なコードを使用すると、テーブルを作成
ファーストを参照してくださいしている
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 ;
そして、その後
<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>
select.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();
ようView.phpを作成します。それから確かにx.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();
なぜあなたはそれを挿入してもらえませんか?どのフレームワークを使用しているのか、データベースの挿入に使用するコードを知ることが役立ちます。 –
私はデータベースにデータを挿入するためにPHPを使用し、私はちょうどフレームワーク、PHPを使用していません。 –