mydownfall00 Posted September 7, 2013 Share Posted September 7, 2013 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? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 7, 2013 Share Posted September 7, 2013 (edited) 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 September 7, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.