xcali Posted July 18, 2010 Share Posted July 18, 2010 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/208107-phpmysql-fetch-array-error/ Share on other sites More sharing options...
wildteen88 Posted July 18, 2010 Share Posted July 18, 2010 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/208107-phpmysql-fetch-array-error/#findComment-1087841 Share on other sites More sharing options...
Psycho Posted July 18, 2010 Share Posted July 18, 2010 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/208107-phpmysql-fetch-array-error/#findComment-1087843 Share on other sites More sharing options...
xcali Posted July 18, 2010 Author Share Posted July 18, 2010 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 : / Quote Link to comment https://forums.phpfreaks.com/topic/208107-phpmysql-fetch-array-error/#findComment-1087862 Share on other sites More sharing options...
wildteen88 Posted July 18, 2010 Share Posted July 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208107-phpmysql-fetch-array-error/#findComment-1087868 Share on other sites More sharing options...
xcali Posted July 18, 2010 Author Share Posted July 18, 2010 PERFECT!!! downloaded "BabelPad" saved in ASCII without BOM, reuploaded the file and it works like a charm!! Thank you so much Wild!!! Quote Link to comment https://forums.phpfreaks.com/topic/208107-phpmysql-fetch-array-error/#findComment-1087887 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.