Unable to jump to row 0 on MySQL result index 3???

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Unable to jump to row 0 on MySQL result index 3???

Post by cturner »

Can someone please tell me why I am getting the following errors:
Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 3 in /home/blu6592/public_html/login.php on line 19

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 4 in /home/blu6592/public_html/login.php on line 23
Here is the code that I am working with:

Code: Select all

require "config.php";

// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
$admin_username = trim($admin_username);
$admin_password = trim($admin_password);

$username_check = "SELECT admin_username FROM diary_admin WHERE admin_username='". mysql_real_escape_string($_POST['admin_username']) . "'";
$username_query = mysql_query ($username_check) or die('Query failed. ' . mysql_error());
$username_result = mysql_result($username_query,0);

$password_check = "SELECT admin_password FROM diary_admin WHERE admin_password ='". mysql_real_escape_string($_POST['admin_password']) . "'";
$password_query = mysql_query ($password_check) or die('Query failed. ' . mysql_error());
$password_result = mysql_result($password_query,0);

    // Each time there's an error, add an error message to the error array
    // using the field name as the key.
    if ($_POST['admin_username']=='')
        $arrErrors['admin_username'] = 'Please enter your username.';
    if ($_POST['admin_password']=='')
        $arrErrors['admin_password'] = 'Please enter your password.';
    if ($_POST['admin_username'] != $username_result)
        $arrErrors['admin_username'] = 'That username was incorrect.';
    if ($_POST['admin_password'] != $password_result)
        $arrErrors['admin_password'] = 'That password was incorrect.';

    if (count($arrErrors) == 0) {         
        // If the error array is empty, there were no errors.
        // Insert form processing here.
        print "Login successful. <a href=diary.php>Click here</a> to continue.";
    } else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($arrErrors as $error) {
            $strError .= "<li>$error</li>";
        }
        $strError .= '</ul></div><p>';
    }
}
Thanks in advance.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

I would think about doing this differently. Your trying to validate a user, so you already have the password and the username, so I think it would be better to just use COUNT(*) WHERE ... The reason you get that error, is because anytime MySQL doesn't find a match and you try to assign a result that doesn't exist you are trying to jump to a row that is not there! mysql_result(); is to get a value from a real existing result that returned a result.


You should do this instead.... (this way your guaranteed to return a result)

Code: Select all

$username_check = "SELECT COUNT(*) FROM diary_admin WHERE admin_username='". mysql_real_escape_string($_POST['admin_username']) . "'";

$username_query = mysql_query ( $username_check ) or die ( 'Query failed. ' . mysql_error () ); 
$username_result = mysql_result ( $username_query, 0, 0 );

If you don't want to do it that way, then check the return count => mysql_num_rows(), before you try to call mysql_result()

Code: Select all

if ( mysql_num_rows ( $username_query ) == 1 )
{
	$username_result = mysql_result($username_query,0);
}
else
{
	echo 'no user exist by that name';
}

I would just combine (username and password) query as you should never tell the person if one is right and the other is not, it makes hacking into your system much easier that way...

Code: Select all

$admin_check = "SELECT COUNT(*) FROM diary_admin WHERE admin_username='". mysql_real_escape_string($_POST['admin_username']) . "' AND admin_password ='". mysql_real_escape_string($_POST['admin_password']) . "'"; 

$admin_query = mysql_query ( $admin_check ) or die ( 'Query failed. ' . mysql_error () );

$admin_result = mysql_result ( $admin_query, 0, 0 );

if ( $admin_result == 1 )
{
	echo " Hi admin user!";
}
else
{
	echo "sorry, you don't know who you are!";
}


printf
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Unable to jump to row 0 on MySQL result index 3???

Post by Chris Corbyn »

cturner wrote:Can someone please tell me why I am getting the following errors:
Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 3 in /home/blu6592/public_html/login.php on line 19

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 4 in /home/blu6592/public_html/login.php on line 23
Query returns zero rows.

mysql_numrows() would tell you if anything was returned ;)
Post Reply