Jump to content

Explain how to fix this fatal error?


jrws

Recommended Posts

Hi guys, me again.

I have a fatal error of

Call to a member function on a non-object

How do you fix these errors?

The specific error that it is saying is about the mysql_num_rows function called Num_Rows; here's the code for it

<?php
function Num_Rows($sql)
    {
        $sql = $this->query($sql);
        $sql = mysql_num_rows($sql) or die(mysql_error());
        return $sql;

    }

?>

And here is a snippit of the code in use (specifically the problem)

<?php 
require_once ('../inc/config.php');
$db = new mysql();
//.......
//This is in a class mind you:
$username_query = "SELECT username FROM user WHERE username = '$username'";
    	$username_queryR = $db->Num_Rows($username_query);
        $email = $this->clean($email);
        $email_query = "SELECT email FROM user WHERE email = '$email'";
        $email_queryR = $db->Num_Rows($email_query);

?>

The question is how do I fix this problem and any future problems such as this?

Link to comment
Share on other sites

well you have used the OOP notation with no visible instantiation of an object...

 

you need to pass the dabase obect in too!!!!

 

Why have you wriiten a function to do what a method already available to you can do????

 

Why are you using $this outside a class????

 

what are you doing?

 

 

Link to comment
Share on other sites

No idea, I am new to PHP and programing in general, trying random stuff. I know my logic is way off, What do you mean by pass the database object?

edit: The num_rows function is inside a class. And what do you mean by there is already a method?

Link to comment
Share on other sites

<?php 
require_once ('../inc/config.php');
$db = new mysql();
//.......
//This is in a class mind you:
$username_query = "SELECT username FROM user WHERE username = '$username'";
$username_queryR = $db->query($username_query);
echo $username_queryR->num_rows;

$email_query = "SELECT email FROM user WHERE email = '$email'";
$email_queryR = $db->query($email_query);
echo $email_queryR->num_rows;

?>

 

what do you mean to achieve with this line? $email = $this->clean($email);

 

answer to question 1.

 

Not being funny but you need to know about scope if you are going to code - even at a basic level....

 

question 2.

 

you should read up on classes and OOP and learn what a 'method' is...

Link to comment
Share on other sites

A method is a function no?

 

well no not really - in this ocntext a method belongs to an object - something you can perform on that object.  AN object (or an instance of a class) needs to be aware of what it can do - this is done by declaring these methods within the objects template (the class).  Unless you have coded something like

<?php
class Myclass
{
private $foo;

protected function__construct()
{
  $this->foo = 'Initiated';
}

private function bar ()
{
  echo $this->foo;
}
}
?>

 

get some books or read some stuff on PHP OOP and you will pick it up v quickly...

Doubt anyone on here will teach you to code!!!!

 

And the point of cleaning the emai is to prevent unsanitary user input.

 

you need something to clean before you call a 'clean' on it and where is the method 'clean' defined?

 

again this will come...

Link to comment
Share on other sites

In the user class. It takes the arguments given, $user, $pass1, $pass2, $email.

It then uses these to perform the rest of the script. I was thinking that the arguments would be from the form users submit, therefore making the $user = $_POST['user'; etc

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.