Page 1 of 1
My SQL session error
Posted: Fri Dec 30, 2011 4:05 pm
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.
Re: My SQL session error
Posted: Fri Dec 30, 2011 6:08 pm
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);
Re: My SQL session error
Posted: Fri Dec 30, 2011 6:37 pm
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?
Re: My SQL session error
Posted: Sat Dec 31, 2011 1:17 am
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);
Re: My SQL session error
Posted: Sat Dec 31, 2011 9:53 am
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

!
Re: My SQL session error
Posted: Mon Jan 02, 2012 1:30 am
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.
Re: My SQL session error
Posted: Mon Jan 02, 2012 1:32 pm
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.
Re: My SQL session error
Posted: Tue Jan 03, 2012 12:57 am
by Christopher