Jump to content

Php/mysql Fetch Array Error


xcali

Recommended Posts

Hey forum

I got an issue I'm trying to fix for the past day and I can't figure out whats wrong.

 

I have a login form that user fills out, hits submit and each time I get the following:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /login.php on line 23

 

Row #23 is the following: $row = mysql_fetch_array ($result, MYSQL_NUM);

 

Here is the full code:

 

<?php

if (isset($_POST['submit']))
  {  //main
     require_once ('dbconnect.php');
    $errors = array();

if (strlen($_POST['username']) > 0)
  { $username=$_POST['username'];  }
else
  { $errors[] = '<p>You forgot to enter your username!</p>';  }

if (strlen($_POST['passwd']) > 0)
    { $pas=$_POST['passwd'];  }
else
    { $errors[] = '<p>You forgot to enter your password!</p>';  }

   if(empty($errors))
  { //empty

    $query  = "SELECT fname, lname FROM tablename WHERE username='$username' AND password='$pas' ";
    $result = mysql_query($query);
    $row    = mysql_fetch_array ($result, MYSQL_NUM);

   if($row)
    {  //row
   ...do whatever
    } //row
   else
    { $errors[] = 'No matches found.';
    }

   } //empty
mysql_close();
} //main
else
   { $errors = NULL;  }
if (!empty($errors))
  { echo '<br><p class="error"><b>Following error(s) occured: </b><br />';
   foreach ($errors as $msg)
  { echo "  $msg"; }
   echo '</p><br><p> Letters are <i>Case Sensitive</i>.';
   echo '<br> Please <a href="login.php">go back</a> and try again.';
   echo '<br> Forgot your password? <a href=fpass.php>Click here</a>.</p>';
  }

?>

Link to comment
Share on other sites

before using mysql_fetch_array you should first make sure mysql_query didn't return false (due to an error) and that your query did actually return a result.

if(empty($errors))
{
$query  = "SELECT fname, lname FROM tablename WHERE username='$username' AND password='$pas' ";
$result = mysql_query($query);

// check that mysql_query did not retrun false
if($result)
{
	// now check that there was a match with the username/password combo
	if(mysql_num_rows($result) == 1)
	{
		// found a match with the username/password combo
		// login user here
	}
	// no matches found, return an error
	else
	{
		echo 'Invalid username/password combination.';
	}
}
// something is wrong, lets find out
else
{
	echo 'Unable proccess request due to an error: ' . mysql_error();
}
}

Link to comment
Share on other sites

Although, this has nothing to do with your error, I would advise using strlen($_POST['fieldname']) to validate the input. Unless there is a valid reason not to, you should always trim() the input. Otherwise a user can enter spaces and it will be considered valid. That assumes that you are also using trim when the values are created and stored in the DB. Also, using empty() is a little more compact and is more "obvious" to someone reading the code:

 

Example:

$username = trim($_POST['username']);
$password = trim($_POST['passwd']);
if (empty($username))
    { $errors[] = '<p>You forgot to enter your username!</p>';  }
if (empty($password))
    { $errors[] = '<p>You forgot to enter your password!</p>';  }

Link to comment
Share on other sites

I knew you geniuses on here would be able to help me :)

It seems that in my dbconnect I commented out the database name I was pulling data from.

 

and i will definitely change my code to TRIM it

 

Problem solved thank you much!

 

now i gotta figure out the problem with the cookies, from the same code as above,

i've added a:

     ...
   $row  = mysql_fetch_array ($result, MYSQL_NUM);
	  if($row)
		  {	 //row
			setcookie('fname', $row[0]);
			setcookie('lname', $row[1]);
			//header("Location: calendar.php");
			exit();
		  } //row
              ...

 

and right off the bat i get the following:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/r/o/frozensite/html/formlogin.php:1) in /home/content/f/r/o/frozensite/html/formlogin.php  on line 27

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/r/o/frozensite/html/formlogin.php:1) in /home/content/f/r/o/frozensite/html/formlogin.php on line 28

 

 

Once again thank you so much for all the help !!

 

I was using the same code a year or 2 ago on a different server and everything worked - now that I moved the files to a new hosting server i keep getting all kinds of errors : /

Link to comment
Share on other sites

On line 1 in formlogin.php you seem to have some form of output. Note anything s considered output if its outside of your <?php ?> tags, or its being echo/print'd etc.

 

If there is nothing before your opening PHP tag then check your files charset encoding setting (usually available in the Save As dialog box) is set to ASCII or UTF8 without BOM

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.