所以基本上,这是我现在的情况。我正在使用xampp(apache friends)为我的localhost,我目前正在使用AJAX和PHP构建一个简单的聊天室窗口。目前,我有两个文件,分别是C:/xampp/htdocs/AJAX CHAT/index.php
和C:/xampp/htdocs/AJAX CHAT/chat.php
。
的index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:www.//w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html" />
<title>Deontray's Chat Room!</title>
<script src="jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript">
function chat_initial() {
var user = document.getElementById("chat_user").value;
$.post('./chat.php', {
stage: "initial",
user: user
}, function(data) {
alert(data);
});
/*
get user
check if taken
hide the initial div
display the primary div
*/
}
</script>
<style type="text/css">
<!-- #chatbox {
background-color: #DDD;
border: 1px solid #000;
width: 700px;
height: 500px;
}
#chatbox #initial {
text-align: center;
margin: auto;
width: 250px;
padding-top: 100px;
}
-->
</style>
</head>
<body>
<div id="chatbox">
<div id="initial">
<table>
<tr align="center">
<td>Enter a username to start chatting:</td>
</tr>
<tr align="center">
<td>
<input type="text" name="chat_user" id="chat_user" style="width: 200px;" />
</td>
</tr>
<tr align="center">
<td>
<br />
<input type="button" value="Enter chat!" onClick="chat_initial();" />
</td>
</tr>
</table>
</div>
<div id="primary"></div>
</div>
</body>
</html>
chat.php
<?php
//connect to MySQL database
mysqli_connect("localhost", "root", "deontray");
mysqli_select_db("tutorials");
//read the stage
$stage = $_POST['stage'];
//primary code
if($stage == "initial"){
//check the username
$user = $_POST['user'];
$query = mysqli_query("SELECT * FROM `chat_active` WHERE user = '$user'");
if (mysqli_num_rows($query) == 0){
$time = time();
//
mysqli_query("INSERT INTO `chat_active` VALUES ('$user', '$time')");
//set the session
$_SESSION['user'] = $user;
echo "good";
}
else
echo "Username Taken";
}
else
echo "Error.";
?>
答案 0 :(得分:2)
使用mysqli
连接数据库$connent_mysqli = mysqli_connect($db_host,$db_username,$db_password, $db_name);
并获取类似的数据
$query = mysqli_query($connent_mysqli, "SELECT * FROM `chat_active` WHERE user = '$user'");