无故无法获取mysqli_num_rows()错误?
问题描述:
所以基本上,这是我现在的情况。我为我的本地主机使用xampp(apache朋友),我目前正在使用AJAX和PHP构建一个简单的聊天室窗口。目前,我有两个文件,分别是C:/xampp/htdocs/AJAX CHAT/index.php
和C:/xampp/htdocs/AJAX CHAT/chat.php
。无故无法获取mysqli_num_rows()错误?
的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);
});
/*
\t \t \t \t get user
\t \t \t \t check if taken
\t \t \t \t hide the initial div
\t \t \t \t display the primary div
\t \t \t \t */
}
</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
\t mysqli_connect("localhost", "root", "deontray");
\t mysqli_select_db("tutorials");
//read the stage
\t $stage = $_POST['stage'];
//primary code
\t if($stage == "initial"){
\t \t //check the username
\t \t $user = $_POST['user'];
\t \t
\t \t $query = mysqli_query("SELECT * FROM `chat_active` WHERE user = '$user'");
\t \t if (mysqli_num_rows($query) == 0){
\t \t \t $time = time();
\t \t \t //
\t \t \t mysqli_query("INSERT INTO `chat_active` VALUES ('$user', '$time')");
\t \t \t //set the session
\t \t \t $_SESSION['user'] = $user;
\t \t \t
\t \t \t echo "good";
\t \t }
\t \t else
\t \t \t echo "Username Taken";
\t }
\t else
\t \t echo "Error.";
?>
答
使用的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'");
,你实际上并没有描述一个问题或提问的事实数据可能与你为什么”没有找到答案。您的代码中是否有任何实际*错误*? – David
“这不是重复的,因为我这样说”和“它不是重复的,因为(1)...(2)...(3)...”。看到你甚至没有尝试用'mysqli_error()'进行探测,或者使用参数绑定。 – mario
这是我的错误:警告:mysql_num_rows期望参数1是资源,布尔给定 –