Jump to content

[SOLVED] Call to a member function query() on a non-object


drumhrd

Recommended Posts

Hello,

 

I am getting this message.

 

Fatal error: Call to a member function query() on a non-object in /var/www/new_form.php on line 59

 

I am queries my db using "$result = $connection->query($query);"

 

variable set:

  $query = "SELECT * FROM users WHERE username='$username'";

 

I do not know why this is erroring out.  I use this same process for all my other db queries.  Can someone help me out here?

 

code:

 

<snip>

 

<?php

// Check for form post submit

if ($_POST["submit"]){

    require_once('db_login.php');

    require_once('DB.php');

    $connection = DB::connect("mysql://$db_username:$db_password@$db_host$db_database");

    if (DB::isError($connection)){

        die ("Could not connect to the database: <br />". DB::errorMessage($connection));

    }

    // Remember to use htmlentities to prevent cross-site scripting vulnerabilities

    $username = $_POST["username"];

    $username=mysql_real_escape_string(get_magic_quotes_gpc(  ) ? stripslashes($username) : $username);

    $password = $_POST["password"];

    $password=htmlentities(get_magic_quotes_gpc(  ) ? stripslashes($password) :$password);

    $email = $_POST["email"];

    $email=htmlentities(get_magic_quotes_gpc(  ) ? stripslashes($password) :$password);

    $phone = $_POST["phone"];

    $phone=htmlentities(get_magic_quotes_gpc(  ) ? stripslashes($phone) : $phone);

    $error = "";

}

 

    if (is_null($username == "")){

        $error .= "Username must not be null.<br />";

    }

    if ($password == ""){

        $error .= "Password must not be null.<br />";

    }

    if ($email == ""){

        $error .= "Email must not be null.<br />";

    }

    if ($phone == ""){

        $error .= "Phone must not be null.<br />";

    }

 

    // Query the posts with categories and user information

    $query = "SELECT * FROM users WHERE username='$username'";

    // Execute the database query

    $result = $connection->query($query);

    if (DB::isError($result)){

        die("Could not query the database: <br />".$query." ".DB::errorMessage($result));

    }

    $user_count = $result->numRows(  );

    if ($user_count > 0) {

        $error .= "Error: Username $username is taken already. Please select another.<br />";

    }

    if ($error){

        echo $error;

    } else {

        echo "Username is available.";

        exit;

    }

 

?>

 

</snip>

The problem is your database connection failed, check the host/user/pass/dbname. If those are correct then there is a problem with the database class that you are using.

 

Basically the error means the DB::connect() function did not return an object.

The problem is because when the script is first ran, whether $_POST['submit'] is set or not, you're still calling $connection->query() method. Since the if statement didn't run before, $connection is undefined, so you're calling a method on an undefined object. Get it?

 

Put that inside the if statement.

Ok  so I placed the entire php portion into the if statement.  Now my page is displaying but it is autopopulating username "root" along with root password "*********"

 

So now this doesn't make sense.  the if ($_POST["submit"]" should == false upon initial page load, so where is the <?php in the form getting the $username and $password from?

 

thanks.

 

 

new code:

 

<html>

<head>

<title>Sample Form</title>

 

<script type="text/javascript" src="source.js"></script>

<script type="text/javascript">

function check_valid(form) {

    var error = "";

    error += verify_username(form.username.value);

    error += verify_password(form.password.value);

    error += verify_phone(form.phone.value);

    error += verify_email(form.email.value);

    if (error != "") {

        alert(error);

        return false;

    }

    return true;

}

</script>

</head>

<body>

<?php

// Check for form post submit

if ($_POST["submit"]){

    require_once('db_login.php');

    require_once('DB.php');

    $connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");

    if (DB::isError($connection)){

        die ("Could not connect to the database: <br />". DB::errorMessage($connection));

    }

    // Remember to use htmlentities to prevent cross-site scripting vulnerabilities

    $username = $_POST["username"];

    $username=mysql_real_escape_string(get_magic_quotes_gpc(  ) ? stripslashes($username) : $username);

    $password = $_POST["password"];

    $password=htmlentities(get_magic_quotes_gpc(  ) ? stripslashes($password) :$password);

    $email = $_POST["email"];

    $email=htmlentities(get_magic_quotes_gpc(  ) ? stripslashes($email) :$email);

    $phone = $_POST["phone"];

    $phone=htmlentities(get_magic_quotes_gpc(  ) ? stripslashes($phone) : $phone);

    $error = "";

 

 

    if (is_null($username == "")){

        $error .= "Username must not be null.<br />";

    }

    if ($password == ""){

        $error .= "Password must not be null.<br />";

    }

    if ($email == ""){

        $error .= "Email must not be null.<br />";

    }

    if ($phone == ""){

        $error .= "Phone must not be null.<br />";

    }

 

    // Query the posts with categories and user information

    $query = "SELECT * FROM users WHERE username='$username'";

    // Execute the database query

    $result = $connection->query($query);

    if (DB::isError($result)){

        die("Could not query the database: <br />".$query." ".DB::errorMessage($result));

    }

 

    $user_count = $result->numRows(  );

    if ($user_count > 0) {

        $error .= "Error: Username $username is taken already. Please select another.<br />";

    }

    if ($error){

        echo $error;

    } else {

        echo "Username is available.";

        exit;

    }

}

?>

 

 

<!-- This script will process the results as well as display the form -->

 

<form action="<?php echo htmlentities($_SERVER["PHP_SELF"]); ?>" method="POST"

onsubmit="return check_valid(this);" id="test1" name="test1">

    <table>

        <tr>

            <td width="30%" align="right">Username:</td>

            <td><input type="text" name="username"value="<?php echo ($username); ?>" />

</td>

        </tr>

        <tr>

            <td align="right">Password:</td>

            <td><input type="password" name="password"value="<?php echo($password); ?>" />

</td>

        </tr>

        <tr>

            <td align="right">Phone:</td>

            <td><input type="phone" name="phone" value="<?php echo($phone); ?>" /></td>

        </tr>

        <tr>

            <td align="right">Email:</td>

            <td><input type="email" name="email" value="<?php echo($email); ?>" /></td>

        </tr>

        <tr>

            <td> </td>

            <td><input type="submit" name="submit" value="Submit" /></td>

        </tr>

    </table>

</form>

</body>

</html>

 

 

 

 

 

Read rule #13 - http://www.phpfreaks.com/page/forum-rules

 

Also, $_POST['submit'] will never equal false by default.

 

In your HTML, you can put an if statement like:

if (!empty($username)) echo $username;

 

Do that for each entry. Because if the user hasn't submitted the form, you have no $username to put there.

this still doesn't make sense..I do not have a root username in my table..PHP is pulling this userID and password from somewhere.

 

table

 

mysql> select * from users;

+---------+------------+-----------+----------+----------------------------------+

| user_id | first_name | last_name | username | password                        |

+---------+------------+-----------+----------+----------------------------------+

|      1 | Michele    | Davis    | mdavis  | 5ebe2294ecd0e0f08eab7690d2a6ee69 |

+---------+------------+-----------+----------+----------------------------------+

1 row in set (0.00 sec)

 

 

I apologize, I am trying to learn PHP from a book..and it's ticking me off that this script doesn't work.

 

did you want me to put that (if) inside the <input> tag. 

 

I am not sure why the initial if statement will never be false by default.  shouldn't it be checking if the submit button has been hit. in which it will == true?  Initially I would expect it to be false.

 


if ($_POST["submit"]){
}
[code]

I get the same result using

<td><input type="text" name="username"value="<?php if (!empty($username)) echo ($username); ?>" />

 

my Username field still has root as user upon initial load.

 

Whatever is happening is happening inside the form.

 

I placed the following code before the form to see if I could catch the username before it's put into the form and the result is "username is "..it is not putting root into the code.

 

username is <?php echo $username; ?>

 

I still have no idea where the php inside the form is getting $username="root".

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.