Jump to content

Archived

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

ToonMariner

OOP help (again)

Recommended Posts

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....[code]<?php
@ $mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}
?>[/code]

And wanted to use the $mysqli object in my login check class which was...[code]<?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']);
  }
}

....

}
?>[/code]

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


[code]<?php
$login = new login_check();
$login->mysqli = $mysqli;
?>
[/code]

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

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)...
[code]<?php
$loginqry = $this->mysqli->query($loginqry);
?>
[/code]
Unfortunately if I try to check the number of rows of the query like so...
[code]<?php
if ($loginqry->num_rows == 1) {
....
}
?>[/code]

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.

Share this post


Link to post
Share on other sites
More than likely your query failed, thereby mysqli-query() would retrun [i]false[/i] and not an object. Try testing it before you try and use it.

eg;

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

Share this post


Link to post
Share on other sites

×

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.