Jump to content

Function error : pulling user id from username


mydownfall00

Recommended Posts

I am attempting to setup a profile page based on the information that the user signs up with. 

(I am following a video online, the code matches but he is on a local server i am using a hosted server idk if issue)

 

On my profile.php page I am receiving the warning : 

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 5 in /home/content/80/11355280/html/core/function/users.php on line 88

 

On my profile page I am trying to use function : user_id_from_username. This function is currently working with my login.

However on profile.php 

<?php
include 'include/widgets/tabletop.php';

	if (isset($_GET['username']) === true && empty($_GET['username']) === false)  {
		$username 	= $_GET['username'];
		$user_id	= user_id_from_username($username); 
		
		echo $user_id;

	} else {
	header('Location: index.php');
	exit();
	}

include 'include/widgets/tablebot.php';
?>

On line 14 I am calling the function I created and attempting to echo out the user id. When I attempt this I receive the warning. 

 

The function is held in users.php and is included on the page. 

 

users.php

function user_id_from_username($username)  {
	$username = sanitize($username);
	return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}

if I would put a or die at the end of the return mysql_result I get the same error but a different index.

 

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 12 in /home/content/80/11355280/html/core/function/users.php on line 88

 

Would an if ( something > 0 ) fix this and how?

Link to comment
Share on other sites

forget you ever saw the mysql_result() statement. it's the only database function that throws an error when there are zero rows in a result set. it also doesn't have any direct replacement in the database libraries that replace the mysql_ library, which is now depreciated and should not be used when writing new code.

 

you should also never nest function calls, when any of the inner functions can fail due to an error. you must always test if a function worked or not before trying to use the data that function is expected to return.

 

your code needs to -

 

1) use either the mysqli or PDO database libraries.

 

2) test if the query worked or failed before attempting to use the result from that query. also, what action do you want the code to take when the query fails due to an error?

 

3) test if the query actually matched any row(s) and take an appropriate action when it doesn't. what value do you want to return when the username doesn't exist, so that the calling code doesn't attempt to use a non-existent id value?

 

4) only after you have determined that the query ran without any errors and that it matched the one expected row, should you fetch (using an actual database _fetch_ statement) and return the id that the query found.

 

edit: summery - the error you are getting could be due to the query failing or the query matching zero rows (the username variable doesn't contain anything or the value it does contain doesn't exist in the table), but the code is the minimal amount that just barely works when everything is perfect and doesn't tell you when, where, or why it didn't produce the expected result.

Edited by mac_gyver
Link to comment
Share on other sites

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.