Jump to content

Recommended Posts

hi

 

I have a problem with my code

 

I get a mysql_fetch_assoc() supplied argument is not a valid MySQL result resource warning when I run my code.

 

its a basic login page..basi form with username and password.......then it posts to login.php. I open a connection to the db, execute a query and check to see whether a user exists.

 

this is the login.php code.....

 

 

<?php

 

$user = &_post['username'];

$user = &_post['PASSWORD'];

 

$connection = mysql_connect("localhost","root","");

 

$sql = "select * from Admin where AdminUserName = '$user' and AdminPassword = '$pass'";

 

$result = mysql_query($sql);

$row = mysql_fetch_assoc($result);

 

if($row('AdminUserName'] == $user && $row['AdminPassword'] == $pass)

{

logged in

}

else

{

invalid user

}

 

?>

 

please help me - not sure where I am going wrong - I executed the SQL statement in phpmyadmin?

 

thanks

 

Link to comment
https://forums.phpfreaks.com/topic/87258-newbie-php-problem-logging-in/
Share on other sites

<?php
  $user = $_POST['username'];
  $pass = $_POST['PASSWORD']; //Are you sure you want uppercase PASSWORD here?

  $connection = mysql_connect("localhost","root","")
    or die("FAILED TO CONNECT TO DATABASE");

  $sql = "SELECT * FROM `admin` WHERE `AdminUserName` = '".mysql_real_escape_string($user)."' LIMIT 1";
  $result = mysql_query($sql)
    or die("Query error: ".mysql_error());
  $row = mysql_fetch_assoc($result);

  //Test password here instead of in query
  if(strcmp($pass,$row['AdminPassword']))
    die("Invalid Password");

  //Continue here
?>

<?php

$user = $_POST['username'];
$pass = $_POST['PASSWORD'];

$connection = mysql_connect("localhost","root","");

$sql = "select * from Admin where AdminUserName = '{$user}' and AdminPassword = '{$pass}'";

$result = mysql_query($sql);

if(mysql_num_rows($result) == 1)
{
  //show admin page
}
else
{
  //go away
}

/* the below logic is worthless, it's covered in your query. */
if($row('AdminUserName'] == $user && $row['AdminPassword'] == $pass)
{

}

 

Try that.

 

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.