Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2009
    Location
    Florida United States
    Posts
    106
    Thanks
    6
    Thanked 2 Times in 2 Posts

    login with sessions

    I keep getting this error and I don't know why?

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\kickado\sign_in.php on line 25

    Code:
    <?php 
      session_start();
      //start session
      
      //clear error message
      $error_msg = "";
      
      //if user is not logged in attempt to log in
      if (!isset($_SESSION['user_id'])){
        if (isset($_POST['submit'])){
          
          //connect to database
          $dbc = mysqli_connect('localhost', 'root', '', 'kickado');
                
          //grab login from form input
          $user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
          $user_password = mysqli_real_escape_string($dbc, trim($_POST['password']));
          
          if (!empty($user_username) && !empty($user_password)){
            //compare the input with whats stored in database
            $query = "SELECT user_id, user_name FROM kickado_user WHERE user_name = '$user_username' AND" . 
            "password = SHA('$user_password')";
            $data = mysqli_query($dbc, $query);
            
            if (mysqli_num_rows($data) == 1){
              //Log in is confirmed set session and redirect to home page
              $row = mysqli_fetch_array($data);
              $_SESSION('user_id', $row('user_id'));
              $_SESSION('username', $row('user_name'));      
              $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
              header('Location: ' . $home_url);
            }else {
              //username and password are incorrect display an error message
              $error_msg = 'Sorry, you must enter a valid username and password to log in';
            }
          }else {
             //username and password are blank display an error message
             $error_msg = 'User name and Password are empty!';
          }      
        }    
      }
    ?>
    <!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" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
      <title>KickADo | Your 30 seconds to fame!</title>   
      <link type = "text/css"
          href = "sign_in.css"
          rel = "stylesheet" />
    </head>
    <body>
    
      <div id = "all">
      <?php include("header.php"); ?>
        <div id = "content">
        <?php
        //if the session is empty display error message and the form
        if (empty($_SESSION['user_id'])){
          echo '<p>' . $error_msg . '</p>';
        ?>    
          <form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>" >
            <label>User Name</label>
            <input type = "text"
                   name ="username"
                   value = "<?php if (!empty($user_username)) echo $user_username; ?>" /><br />
            <label>Password</label>
            <input type = "password"
                   name ="password" /><br />
            <input type = "submit"
                   value = "login"
                   name = "submit" />
          </form>
        </div>
      <?php include("footer.php"); ?>  
      </div>
      <?php
        }
    else{
      //confirm login success
      echo ('<p class="login">You are logged in as' . $_SESSION['username'] . '.</p>');
    }
        ?>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,452
    Thanks
    275
    Thanked 32 Times in 31 Posts
    is this backwards

    Code:
    $data = mysqli_query($dbc, $query);
    should it not be your query name then your db connection

    like this

    Code:
     $data = mysqli_query($query, $dbc);

    is that it, im not sure but i think its backwards...

  • #3
    Regular Coder
    Join Date
    Nov 2009
    Location
    Florida United States
    Posts
    106
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Nope that didnt help

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    I assume there was a connection error.

    As the PHP manual itself suggests, put this after your connect line and see what it says:

    PHP Code:
    if (mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();

    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Regular Coder
    Join Date
    Nov 2009
    Location
    Florida United States
    Posts
    106
    Thanks
    6
    Thanked 2 Times in 2 Posts
    I tryed that and my db connection is good I did some reading on xampp and found that many people have problems with using sessions on xampp. I will try to upload to my normal server and see if that works


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •