Welcome to Dream.In.Code
Become an Expert!

Join 150,136 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,222 people online right now. 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
1 Aug, 2008 - 01: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
+Quote Post

mocker
RE: PHP MYSQL And A Bad Resource Id
1 Aug, 2008 - 01:24 PM
Post #2

D.I.C Regular
Group Icon

Joined: 14 Oct, 2007
Posts: 290



Thanked: 17 times
Dream Kudos: 25
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
+Quote Post

duffsstuff
RE: PHP MYSQL And A Bad Resource Id
1 Aug, 2008 - 01: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
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:57AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month