drumhrd Posted May 3, 2009 Share Posted May 3, 2009 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> Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/ Share on other sites More sharing options...
the182guy Posted May 3, 2009 Share Posted May 3, 2009 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. Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-824825 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 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. Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-824945 Share on other sites More sharing options...
drumhrd Posted May 4, 2009 Author Share Posted May 4, 2009 this doesn't make sense..I use the same $query and $result for other scripts and they work. Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825327 Share on other sites More sharing options...
drumhrd Posted May 4, 2009 Author Share Posted May 4, 2009 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> Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825335 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 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. Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825347 Share on other sites More sharing options...
drumhrd Posted May 4, 2009 Author Share Posted May 4, 2009 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] Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825360 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 You end a tag with a tag. Yes, like this: <td><input type="text" name="username" value="<?php if (!empty($username)) echo ($username); ?>" /> Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825363 Share on other sites More sharing options...
drumhrd Posted May 4, 2009 Author Share Posted May 4, 2009 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". Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825380 Share on other sites More sharing options...
drumhrd Posted May 4, 2009 Author Share Posted May 4, 2009 since technically..my original issue is fixed..I am going to close this post and start a new one "PHP putting "root" user into html form" thanks for your help so far! Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825383 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Do you have $username in any of your include files? Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825384 Share on other sites More sharing options...
drumhrd Posted May 4, 2009 Author Share Posted May 4, 2009 good idea..I replaced all $username with $form_username....getting the same result. Link to comment https://forums.phpfreaks.com/topic/156641-solved-call-to-a-member-function-query-on-a-non-object/#findComment-825389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.