Jump to content


Photo

OOP help (again)


  • Please log in to reply
1 reply to this topic

#1 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 15 March 2006 - 11:46 AM

OK If anybody read my post below regarding objects being used in otehr objects.....

I THINK i solved my problem there....

I had the following....
<?php
@ $mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}
?>

And wanted to use the $mysqli object in my login check class which was...
<?php
class login_check {
function __construct() { // initialize class properties.
// ensure no mysql injection in usernamer or password        
  if (isset($_POST['username'], $_POST['password'])) {
   $this->username = $mysqli->real_escape_string($_POST['username']);
   $this->password = $mysqli->real_escape_string($_POST['password']);
  }
}

....

}
?>

Now it did not recognize mysqli as an object so here is what I did...

I removed the escape string in te constructor and shifted it to the method that queries the database. In the section of my code that executes the login check i did this..


<?php
$login = new login_check();
$login->mysqli = $mysqli;
?>

This allowed me to do this to escape the srting...
<?php
$this->username = $this->mysqli->real_escape_string($_POST['username']);
?>

Which works quite nicely.

OK The problem is now that the query shoudl return an object (The query string in this example is set but I haven't shown it)...
<?php
$loginqry = $this->mysqli->query($loginqry);
?>
Unfortunately if I try to check the number of rows of the query like so...
<?php
if ($loginqry->num_rows == 1) {
....
}
?>

I get the following error....

Notice: Trying to get property of non-object in C:\Program Files\Apache Group\Apache2\htdocs\f_m_cms\classes\login.php on line 70

line 70 being the if statement..

ANy help much appreciated.
follow me on twitter @PHPsycho

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 March 2006 - 12:55 PM

More than likely your query failed, thereby mysqli-query() would retrun false and not an object. Try testing it before you try and use it.

eg;

if ($loginqry) {
  if ($loginqry->num_rows == 1) {
  ....
  }
} else {
  echo "query fialed".$mysqli->error;
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users