Jump to content

Probem with login.


creek16

Recommended Posts

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'); 	
}
*/	
?>

Link to comment
Share on other sites

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');
}
*/
?>

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.