creek16 Posted June 21, 2011 Share Posted June 21, 2011 Hi PHP newbie here. I'm Having problem with my simple Log-in Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\forum\checklogin.php on line 15 This is my code: <?php $con = mysql_connect("localhost", "Root", ""); if(!$con) { die("Failed to connect" . mysql_error()); mysql_close($con); } mysql_select_db("forum", $con); $un = $_POST['un']; $pw = $_POST['pw']; $result = mysql_query("Select * FROM tblAccount WHERE username='$un' AND password='$pw'"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } /* if (mysql_num_rows("$result") > 0) { header('location:main.php'); } else { header('location:login.php?error=1'); } */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/239950-probem-with-login/ Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 That error indicates that the query failed and returned a boolean FALSE to mysql_fetch_array(). You say you're new to php, so now would be a good time to get in the habit of separating the query string from the query execution and using some basic error handling, especially while developing. You would not want to echo the query string or the output of mysql_error() to the screen on a live site, however. For a live site, you'd want to echo a generic "Sorry, there was a database error." type of message, and log the actual error. <?php $con = mysql_connect("localhost", "Root", ""); if(!$con) { die("Failed to connect" . mysql_error()); mysql_close($con); } mysql_select_db("forum", $con); $un = $_POST['un']; $pw = $_POST['pw']; $query = "Select * FROM tblAccount WHERE username='$un' AND password='$pw'"; if( $result = mysql_query($query) ) { while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } } else { echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>'; } /* if (mysql_num_rows("$result") > 0) { header('location:main.php'); } else { header('location:login.php?error=1'); } */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/239950-probem-with-login/#findComment-1232587 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.