samedi 9 mai 2015

Getting a mysqli_num_rows() error for no reason?

So basically, this is my situation right now. I am using xampp(apache friends) for my localhost and I am currently in the process of building a simple chat room windows using AJAX and PHP. Currently, I have two files and those are C:/xampp/htdocs/AJAX CHAT/index.php and C:/xampp/htdocs/AJAX CHAT/chat.php.

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://ift.tt/kkyg93">
<html xmlns="http:http://ift.tt/lH0Osb">

<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.";
?>
This is my error: Warning: mysql_num_rows expects parameter 1 to be resource, boolean given.

Aucun commentaire:

Enregistrer un commentaire