Lodius2000 Posted May 25, 2008 Share Posted May 25, 2008 this may be the total wrong place to put this but it does involve objects I know very little about oop so here goes i am getting an error message when I submit a valid username and password in my login page it tells me: Please enter a valid password. Note: I user PEAR DB and the password is stored crypt()-ed the password validation //check that password matches username $encrypted_password = $db->getOne("SELECT password FROM users WHERE username = '$_POST[username]'"); if ($encrypted_password != crypt($_POST['password'], $encrypted_password)){ $errors[] = 'Please enter a valid password.'; } i tried replacing getOne with query but it returned a php error that said "Warning: crypt() expects parameter 2 to be string, object given in admin/index.php on line 75," which is the if() statement i have also tried $encrypted_password = $db->getOne("SELECT password FROM users WHERE username = '".$_POST['username']."'"); that didnt work, I didnt think it would because my username verification looks like //check that the username exists $q = $db->query("SELECT username FROM users WHERE username = '$_POST[username]'"); if ($q->numrows() == 0 ){ $errors[] = 'Please enter a valid username.'; } and it works just fine the really frustrating thing about this is that I copied the password varification directly out of "learning php 5" by o'reilly and it doesnt work so i guess my question is how do you convert an object into a string, so that i can use query instead of getOne or... if you know what the problem is please let me know how to fix it Quote Link to comment https://forums.phpfreaks.com/topic/107237-converting-an-object-into-a-string/ Share on other sites More sharing options...
Daniel0 Posted May 26, 2008 Share Posted May 26, 2008 Try $encrypted_password = (string) $db->getOne("SELECT password FROM users WHERE username = '$_POST[username]'"); If that doesn't work, then (on your original code), try to put var_dump($encrypted_password); before the if and post what it says here. Quote Link to comment https://forums.phpfreaks.com/topic/107237-converting-an-object-into-a-string/#findComment-550064 Share on other sites More sharing options...
aschk Posted June 5, 2008 Share Posted June 5, 2008 To answer the title question (rather than the specifics of this database object question). You can override the __toString() method in PHP to provide a string value to the class in question. e.g. <?php class test { public function __toString(){ return "this is my class"; } } $test = new test(); echo $test; ?> Try it and see Quote Link to comment https://forums.phpfreaks.com/topic/107237-converting-an-object-into-a-string/#findComment-558146 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.