Jump to content

Archived

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

tomfmason

Mysql_result(): supplied argument is not a valid MySQL result

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
I tried echoing $login_match and it is returning 0.

here is what I have for login match

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

I am lost ???

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.