Jump to content

OOP help (again)


ToonMariner

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.
Link to comment
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]
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.