My SQL session error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gguppies
Forum Newbie
Posts: 4
Joined: Fri Dec 30, 2011 3:48 pm

My SQL session error

Post by gguppies »

Hi guys,
I am getting the following error : Warning: mysql_fetch_assoc(): 2 is not a valid MySQL result resource in /hermes/bosweb26b/b2308/sl.lago/public_html/trial1234567890/index.php on line 25
Idk how to fix it...
This is my code:

Code: Select all

Select allmysql_connect("", "", "") or die(mysql_error()); 

mysql_select_db("") or die(mysql_error()); 

            function is_valid_user()
            {
                  if(isset($_SESSION['user-ID']))
                  {
                     echo "<a href='members.php'>" .$row['firstname']. "</a>";
                 }
               if(!isset($_SESSION['user-ID']))
                  {
                     echo "<a href='login.php'>Login</a>";
                 }
            }
            $query = mysql_query("SELECT * FROM users WHERE userID = '".$_POST['userID']."'")or die(mysql_error());
            $result = $query;
            mysql_free_result($result);
            $row = mysql_fetch_assoc($result);
            ?>

Line 25 is:

Code: Select all

 $row = mysql_fetch_assoc($result);
Please help me out, I really need to get this to work ASAP.
Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: My SQL session error

Post by Christopher »

Because you are freeing the result set before you attempt to fetch. Remove these lines and use $query for the fetch, you don't need them:

$result = $query;
mysql_free_result($result);
(#10850)
gguppies
Forum Newbie
Posts: 4
Joined: Fri Dec 30, 2011 3:48 pm

Re: My SQL session error

Post by gguppies »

Is this what you meant?

Code: Select all

Select allmysql_connect("", "", "") or die(mysql_error()); 

mysql_select_db("") or die(mysql_error()); 

            function is_valid_user()
             {
                   if(isset($_SESSION['user-ID']))
                   {
                      echo "<a href='members.php'>" .$row['firstname']. "</a>";
                  }
                if(!isset($_SESSION['user-ID']))
                   {
                      echo "<a href='login.php'>Login</a>";
                  }
             }
             $query = mysql_query("SELECT * FROM users WHERE userID = '".$_POST['userID']."'")or die(mysql_error());
             mysql_free_result($query);
             $row = mysql_fetch_assoc($query);
             ?>
I changed the code to this, but the error still appears. What should I do?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: My SQL session error

Post by Christopher »

Don't free the results before you fetch the records:

Code: Select all

             $query = mysql_query("SELECT * FROM users WHERE userID = '".$_POST['userID']."'")or die(mysql_error());
             $row = mysql_fetch_assoc($query);
(#10850)
gguppies
Forum Newbie
Posts: 4
Joined: Fri Dec 30, 2011 3:48 pm

Re: My SQL session error

Post by gguppies »

Awesome! that worked! Now I have another problem though. For some reason my login code doesn't work. It is able to check the database and everything, but when the password is correct it does not log the user in. I don't know where the error could be, but probably somewhere in the part where it checks to see if the password matches the username. Here's the code:

Code: Select all

<?php 

// Connects to your Database 

mysql_connect("", "", "") or die(mysql_error()); 

mysql_select_db("") or die(mysql_error()); 


//Checks if there is a login cookie

 if(isset($_COOKIE['ID_my_site']))


 //if there is, it logs you in and directes you to the members page

 { 
   $username = $_COOKIE['ID_my_site']; 

   $pass = $_COOKIE['Key_my_site'];

        $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());

    while($info = mysql_fetch_array( $check ))    

       {

       if ($pass != $info['password']) 

         {

                    }

       else

          {

          header("Location: index.php");



          }

       }

 }


 //if the login form is submitted 

if (isset($_POST['submit'])) { // if form has been submitted



 // makes sure they filled it in

    if(!$_POST['username'] | !$_POST['pass']) {

       function validate(){

 if(username == ''){
 echo('• You need to fill in your username.');
 ?>
 

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

<table border="0"> 

<tr><td colspan=2><h1>Login</h1></td></tr> 

<tr><td>Username:</td><td> 

<input type="text" name="username" maxlength="40"> 

</td></tr> 

<tr><td>Password:</td><td>
 
<input type="password" name="pass2" maxlength="50" /></td></tr> 

<tr><td colspan="2" align="right"> 

<input type="submit" name="submit" value="Login"><br/> 

<a href="forgot1.php" class="forgot">Forgot your username?</a><br/>
 <a href="forgot2.php" class="forgot">Forgot your password?</a>

 </td></tr> 

</table> 

</form> 

<?php
 }
 if(pass == ''){
 echo('• Please fill in a password.');
 ?>
  <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

<table border="0"> 

<tr><td colspan=2><h1>Login</h1></td></tr> 

<tr><td>Username:</td><td> 

<input type="text" name="username" maxlength="40"> 

</td></tr> 

<tr><td>Password:</td><td> 

<input type="password" name="pass" maxlength="50"> 

</td></tr> 

<tr><td colspan="2" align="right"> 

<input type="submit" name="submit" value="Login"><br/> 

<a href="forgot1.php" class="forgot">Forgot your username?</a><br/>
 <a href="forgot2.php" class="forgot">Forgot your password?</a>

 </td></tr> 

</table> 

</form> 

<?php
 }
 } 

   }

    // checks it against the database



    if (!get_magic_quotes_gpc()) {

       $_POST['email'] = addslashes($_POST['email']);

    }

    $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());



 //Gives error if user dosen't exist

 $check2 = mysql_num_rows($check);

 if ($check2 == 0) {
  echo('• That user does not exist in our database. <a href="register.php">Click Here to Register</a>, or try again below.');

 ?>
 <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

<table border="0"> 

<tr><td colspan=2><h1>Login</h1></td></tr> 

<tr><td>Username:</td><td> 

<input type="text" name="username" maxlength="40"> 

</td></tr> 

<tr><td>Password:</td><td> 

<input type="password" name="pass" maxlength="50"> 

</td></tr> 

<tr><td colspan="2" align="right"> 

<input type="submit" name="submit" value="Login"><br/> 

<a href="forgot1.php" class="forgot">Forgot your username?</a><br/>
 <a href="forgot2.php" class="forgot">Forgot your password?</a>

 </td></tr> 

</table> 

</form> 
<?php

             }

 while($info = mysql_fetch_array( $check ))    

 {

 $_POST['pass'] = stripslashes($_POST['pass']);

    $info['password'] = stripslashes($info['password']);

    $_POST['pass'] = md5($_POST['pass']);



 //gives error if the password is wrong

    if ($_POST['pass'] != $info['password']) {

      echo('• Incorrect password, please try again.');
      
      ?>
        <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

<table border="0"> 

<tr><td colspan=2><h1>Login</h1></td></tr> 

<tr><td>Username:</td><td> 

<input type="text" name="username" maxlength="40"> 

</td></tr> 

<tr><td>Password:</td><td> 

<input type="password" name="pass" maxlength="50"> 

</td></tr> 

<tr><td colspan="2" align="right"> 

<input type="submit" name="submit" value="Login"><br/> 

<a href="forgot1.php" class="forgot">Forgot your username?</a><br/>
 <a href="forgot2.php" class="forgot">Forgot your password?</a>

 </td></tr> 

</table> 

</form> 

      
<?php
    }
   
   else 

{ 


// if login is ok then we add a cookie 
     session_start();
      $_SESSION['userID'] = $info['userID']; 
     $_SESSION['userID'] = 1;

     $_POST['username'] = stripslashes($_POST['username']); 

    $hour = time() + 3600; 

setcookie(ID_my_site, $_POST['username'], $hour); 

setcookie(Key_my_site, $_POST['pass'], $hour);    



//then redirect them to the members area 

header("Location: index.php"); 

} 

} 

} 

else 

{    



// if they are not logged in 

?> 

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

<table border="0"> 

<tr><td colspan=2><h1>Login</h1></td></tr> 

<tr><td>Username:</td><td> 

<input type="text" name="username" maxlength="40"> 

</td></tr> 

<tr><td>Password:</td><td> 

<input type="password" name="pass" maxlength="50"> 

</td></tr> 

<tr><td colspan="2" align="right"> 

<input type="submit" name="submit" value="Login"><br/> 

<a href="forgot1.php" class="forgot">Forgot your username?</a><br/>
 <a href="forgot2.php" class="forgot">Forgot your password?</a>

 </td></tr> 

</table> 

</form> 

<?php 

} 



?> 
This is a lot of code, but idk exactly where the problem is. Thanks for the help :D !
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: My SQL session error

Post by Christopher »

User mysql_fetch_assoc() if you want to access elements by name like $info['password'].

I would recommend reading through all the MySQL function pages in the manual to get a feel for what is available. There are many excellent examples there.
(#10850)
gguppies
Forum Newbie
Posts: 4
Joined: Fri Dec 30, 2011 3:48 pm

Re: My SQL session error

Post by gguppies »

Hi Christopher,
I would love to look through that, but the only problem is that idk how to get one and if I do have it how to get to it. So far I have been relying on some websites, forums, and some really basic programming knowledge to come up with my php code. I bet beeing able to look at that would make my life a whole lot easier.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: My SQL session error

Post by Christopher »

(#10850)
Post Reply