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
https://forums.phpfreaks.com/topic/239950-probem-with-login/
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
https://forums.phpfreaks.com/topic/239950-probem-with-login/#findComment-1232587
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.