Jump to content

Recommended Posts

I have am trying(unsuccessfully) to create a login script. As suggested, I tried it with magic_quotes. I get the following error.

[code]Warning: mysql_result(): supplied argument is not a valid MySQL result resource in test_login.php on line 13[/code]

and here is the code

[code=php:0]<?php
include ('includes/db.php');
array_pop($_POST);
if ( get_magic_quotes_gpc() ) {
    $_POST= array_map('stripslashes', $_POST);
}

$username= mysql_real_escape_string(trim($_POST['username']));
$password= mysql_real_escape_string(trim($_POST['password']));

$sql= sprintf("SELECT COUNT(*) AS login_match FROM `users` WHERE `username` = '%s' AND `user_password`= '%s'", $username, $password);
$res= mysql_query($sql);
$login_match= mysql_result($res, 0, 'login_match');

if ( $login_match == 1 ) {
    echo "This test worked";
} else {
    echo "This test did not work";
// not logged in
}
?>[/code]


line 13
[code=php:0]$login_match= mysql_result($res, 0, 'login_match');[/code]



I am wondering if anyone can suggest as to why this is happening?
it was a simple error. I changed this line

[b]from:[/b]

[code=php:0]$res= mysql_query($sql);[/code]

[b]to[/b]
[code=php:0]$res= mysql_query($sql) or die(mysql_error());[/code]

This told me that user_password was not valid..lol. A simple error
I am running into another problem. It is not logining me in. I keep getting the error message

[code=php:0]} else {
    echo "This test did not work";[/code]

Any suggestions as to why this is not loging in?
LOL. It was rather simple. I needed to use mysql_num_rows instead of mysql_result. Now I need to work on the sessions.

[b]*The Fix:[/b]
[code=php:0]<?php
include ('includes/db.php');
array_pop($_POST);
if ( get_magic_quotes_gpc() ) {
    $_POST= array_map('stripslashes', $_POST);
}

$username= mysql_real_escape_string(trim($_POST['username']));
$password= mysql_real_escape_string(trim($_POST['password']));

$sql= sprintf("SELECT COUNT(*) AS login_match FROM `users` WHERE `username` = '%s' AND `password`= '%s'", $username, $password);
$res= mysql_query($sql) or die(mysql_error());
$login_match= mysql_num_rows($res) or die(mysql_error());

if ( $login_match == 1 ) {
    echo 'This test worked';
} else {
    print_r ($login_match);
// not logged in
}
?>[/code]

I hope this helps someone else in the future
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.