Help - Search - Members - Calendar
Full Version: Warning: Mysql_fetch_array() Expects Parameter 1 To Be Resource, Boolean Given In E:\wamp\www\rop\login.php On Line 22
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
kaetta
Yes, at the topic's title, you knew what's wrong (sorry, I don't know how to name it ! T_T). Now, I'll show you my script :

This is a script to login.

CODE
<?php
if (isset($_POST["submit"])) {
    include("../connect-rop.php");
    $message = NULL;
    if(empty($_POST["username"])) {
        $u = FALSE;
        $message .= "You have to enter your username !<br />";
    }
    else {
        $u = stripslashes(trim($_POST["username"]));
    }
    if(empty($_POST["password"])) {
        $p = FALSE;
        $message .= "You have to enter your password !<br />";
    }
    else {
        $p = stripslashes(trim($_POST["password"]));
    }
    if($u&&$p) {
        $query = "SELECT user_id, username FROM profile WHERE username = '$u' AND password = PASSWORD('$p')";
        $result = @mysql_query($query);
        $row = mysql_fetch_array($result);
        if($row){
            session_start();
            $username = $row["username"];
            $user_id = $row["user_id"];
            session_register(username, user_id);
            header("location : http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/index.php");
            exit();
        }
        else {
            $message .= "Your username or password have been entered was wrong !<br />";
        }
    }
}    

$page_title = "Login";
include("template/header.php");
if(empty($message)) {
    echo "<font color=red>So sorry, this page is only for ROP members !</font>";
}
else {
    echo "<font color=red>";
    echo $message;
    echo "</font>";
}
?>
<fieldset><legend>Login</legend>
<table width="260" border="0">
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<tr><td><b>Username :</b></td> <td align="right"><input type="text" name="username" maxlength="30" value="<?php if(isset($_POST["username"])){echo $_POST["username"];}?>" /></td></tr>
<tr><td><b>Password :</b></td> <td align="right"><input type="password" name="password" maxlength="16"/></td></tr>
</table><br /><br />
<input type="submit" name="submit" value="Login"  /><input type="reset" value="Rewrite" />
</form>
</fieldset>
<?php
include("template/footer.php");
?>


And then, when I enter the username and password (even it's wrong or right), it's always appear the message "Your username or password have been entered was wrong !", and then, the error appeared on the top of the page : Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in E:\wamp\www\rop\login.php on line 22.

Nah... I don't know what did I wrong, and what I have to do !

Please, help me. sad.gif(
swordz
If you're going to suppress errors, you're not going to get the full picture.

The mysql_query has failed. Remove the @ to find out why.

swordz
kaetta
@ swordz : I fixed into like this (just the fixed part) :

CODE
$result = mysql_query($query) or die(mysql_error());


And the finally result just... still be that, no more. ' '
kaetta
[I didn't really want to spam]

And how wonderful for today, that warning wasn't appear again, everything is just fine until I can't login with "Your username or password have been entered was wrong !". Even I have entered correctly ! Then, I tried to fix this line :

CODE
$query = "SELECT user_id, username FROM profile WHERE username = '$u' AND password = PASSWORD('$p')";


into

CODE
$query = "SELECT user_id, username FROM profile WHERE username = '$u' AND password = '$p'";


And copy the password that MySQL has provided me (the password after coded by PASSWORD() function).

And the result is the white page with this :

QUOTE
Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0


Nah... How stupid am I. =='' Help me. T^T
Bogey
CODE
            session_start();
            $username = $row["username"];
            $user_id = $row["user_id"];
            session_register(username, user_id);

Remove session_register... you don't need that.

At the beginning of everyfile add session_start(); to allow for the use of sessions.
kaetta
@ Bogey :

QUOTE
Remove session_register... you don't need that.


So... How can I get the $_SESSION["username"] and $_SESSION["password"] ? :-/

QUOTE
At the beginning of everyfile add session_start(); to allow for the use of sessions.


Thank you very much, I forgot it. smile.gif

Even I fixed like what you said (and even if I didn't remove the session_register), "Your username or password have been entered was wrong !" still appear. ' ' (only it XD)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.