Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,536 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,254 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



PHP MYSQL and a Bad Resource Id

 
Reply to this topicStart new topic

PHP MYSQL and a Bad Resource Id

duffsstuff
post 1 Aug, 2008 - 02:13 PM
Post #1


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


I am logging in a user with PHP and have everything working but getting the session to set properly with the value from the id row. My database looks similar to this.

---------------------------------------------------------|
| id | email | password | name |
| 7 | someone@domain.com | secret | Joe |
---------------------------------------------------------|

Here is the logic on my login page. The form is working and setting $_SESSION['id'] but it doesn't have a value.

CODE

session_start();

if(array_key_exists('login', $_POST)) {
     /* Make inputs safe to use. */
     $email = htmlspecialchars($_POST['email']);
     $password = htmlspecialchars($_POST['password']);

     $errors = array();
        
     $dbpassword = mysql_query('SELECT password FROM users WHERE email=(\''.$email.'\')');
     if($dbpassword != crypt($password, $dbpassword)) {
          $errors[] = 'Please enter a valid email and password.';
     } else {
          $userid = mysql_query('SELECT `id` FROM `users` WHERE `email` = \''.$email.'\'');
          $_SESSION['id'] = $userid;
          header('Location: home.php?');
     }
}

User is offlineProfile CardPM

Go to the top of the page


mocker
post 1 Aug, 2008 - 02:24 PM
Post #2


D.I.C Head

**
Joined: 14 Oct, 2007
Posts: 177



Thanked 6 times
My Contributions


bad result id means that for one reason or another your mysql_query line does not work. I am assuming that you have an active mysql connection before this point in the code. It is generally, as in always, a good idea to check that your mysql commands were succesful before blindly relying on their results.

if(!$userid){ print "oh no an error: ".mysql_error(); }

Also, mysql_query returns a resource id. You need to do something like:
$my_result = mysql_query('SELECT `id` FROM `users` WHERE `email` = \''.$email.'\'');
$my_row = mysql_fetch_assoc($my_result);

$_SESSION['id'] = $my_row['id'];
User is offlineProfile CardPM

Go to the top of the page

duffsstuff
post 1 Aug, 2008 - 02:33 PM
Post #3


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


Thankyou for your help. I have it working now!!!!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 09:46PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month