2015-05-09 94 views
-2

所以基本上,這是我現在的情況。我爲我的本地主機使用xampp(apache朋友),我目前正在使用AJAX和PHP構建一個簡單的聊天室窗口。目前,我有兩個文件,分別是C:/xampp/htdocs/AJAX CHAT/index.phpC:/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."; 
 
?>
這是我的錯誤: Warning: mysql_num_rows需要參數1爲資源,布爾給定。

+3

,你實際上並沒有描述一個問題或提問的事實數據可能與你爲什麼」沒有找到答案。您的代碼中是否有任何實際*錯誤*? – David

+1

「這不是重複的,因爲我這樣說」和「它不是重複的,因爲(1)...(2)...(3)...」。看到你甚至沒有嘗試用'mysqli_error()'進行探測,或者使用參數綁定。 – mario

+0

這是我的錯誤:警告:mysql_num_rows期望參數1是資源,布爾給定 –

回答

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'");