Jump to content

converting an object into a string


Lodius2000

Recommended Posts

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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 :)

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.