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
https://forums.phpfreaks.com/topic/107237-converting-an-object-into-a-string/
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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.