Login Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huda89
    New Member
    • Oct 2012
    • 31

    Login Problem

    Hi, i do some code in order to learn login function and i find the code from internet, but when i run the code this error is appear

    >>Warning: mysql_fetch_arr ay() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs \login2\login.p hp on line 12

    I do not know what the problem is, can someone help me please?

    Code:
    <?php
    include("config.php");
    session_start();
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
    // username and password sent from Form 
    $myusername=addslashes($_POST['username']); 
    $mypassword=addslashes($_POST['password']); 
    
    $sql="SELECT id FROM admin WHERE username='$myusername' and passcode='$mypassword'";
    $result=mysql_query($sql);
    $row=mysql_fetch_array($result);
    $active=$row['active'];
    $count=mysql_num_rows($result);
    
    
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1)
    {
    session_register("myusername");
    $_SESSION['login_user']=$myusername;
    
    header("location: welcome.php");
    }
    else 
    {
    $error="Your Login Name or Password is invalid";
    }
    }
    ?>
    <form action="" method="post">
    <label>UserName :</label>
    <input type="text" name="username"/><br />
    <label>Password :</label>
    <input type="password" name="password"/><br/>
    <input type="submit" value=" Submit "/><br />
    </form>
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Are you sure your connection is set up correctly in config.php?

    Comment

    • huda89
      New Member
      • Oct 2012
      • 31

      #3
      I think so, because i just copy it from internet, and i change it according to my connection. Below the code for config.php

      Code:
      <?php
      $mysql_hostname = "localhost";
      $mysql_user = "root";
      $mysql_password = "";
      $mysql_database = "trial";
      $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
      or die("Opps some thing went wrong");
      mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
      ?>

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Are you sure the query returns something? You should check that mysql_query() is returning true before continuing with the rest of your code. You should also check row count before fetching from it in case there are no records.

        Comment

        • huda89
          New Member
          • Oct 2012
          • 31

          #5
          ok, thank you :)
          let me check.

          Comment

          • sharmahemal
            New Member
            • Jul 2012
            • 19

            #6
            Please try it this php code
            if your connection file is correct after you will use it
            Code:
            <?php
            	include("connection.php");
            	$Ocon = new connection();
            	$Ocon->connect(); 
            	if(isset($_REQUEST['btnlogin']))
            	{
            		$username = $_REQUEST['username'];
            		$password = $_REQUEST['password'];
            		$ssQuery = " SELECT * FROM admin WHERE username = '".$username."' AND password = '".$password."' ";
            		$result = mysql_query($ssQuery); 
            		$amInfo = $Ocon->fetch_data($result);
            		if($username== $amInfo[0]['username'] AND $password==$amInfo[0]['password'])
            		{
            			 $_SESSION['username']=$_POST['username'];
            			 header('Location: home.php') ;
            			exit;
            		}
            		else
            		{
            			header( 'Location: index.php?msg=invalid username or password') ;
            			
            		}
            	}
            	ob_flush();
            ?>

            //connection.php


            Code:
            $mysql_hostname = "localhost";
            $mysql_user = "root";
            $mysql_password = "";
            $mysql_database = "trial";
            $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
            or die("Opps some thing went wrong");
            mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
            Last edited by Rabbit; Nov 9 '12, 04:15 PM. Reason: Please use code tags when posting code.

            Comment

            • huda89
              New Member
              • Oct 2012
              • 31

              #7
              Thank You sharmahemal :)

              Comment

              Working...