AdRock Posted August 22, 2008 Share Posted August 22, 2008 I am having a nightmare with my database classes They work fine when ii want to include them on a normal page and output some results but i want to use it within a function to recover a lost password. I have a function that is called when the form is submitted and i need to query the database to see if the email exists in the database. I know this function works when i use mysql_connect etc but i want to use my class. I've done a var_dump of the $db object and this is the output Notice: Could not connect to server in d:\Apache\htdocs\copy of working\php\database\MySQL.php on line 80 object(MySQL)#4 (6) { ["host"]=> NULL ["dbUser"]=> NULL ["dbPass"]=> NULL ["dbName"]=> NULL ["dbConn"]=> bool(false) ["connectError"]=> bool(true) } All my php scripts including the functions are in a folder called php and inside the php folder is a folder called database which holds my database class and connection file. This is the function that is causing the problems....if i can get this working, the rest will all work (which i haven't tried yet cos i know they'll fail) /** * The forgot_password function is called when the user submits their email via the forgotten password page * If the email address is found in the database, create a new random password and email it to the user */ function forgot_password($email) { require_once("php/database/connection.php"); require_once("php/database/MySQL.php"); // Connect to the database and grab the email $db = & new MySQL($host,$dbUser,$dbPass,$dbName); var_dump($db); $sql = "SELECT username, email FROM users WHERE email='$email' LIMIT 1"; $result = $db->query($sql); $err = $result->size(); if($err == 0) { return 'Sorry! Email address not found in our database.'; } else { // Create variables for inclusion in the email $username = $result['username']; $email_address = $result['email']; // Create a new random password $random_password = makeRandomPassword(); // Create a new salt $salt = generate_salt(); // Encrypt the random password, combine it with the salt and encrypt it agin $encrypted = md5(md5($random_password).$salt); // Update the database with the new password $sql = "UPDATE users SET password='$encrypted' WHERE email='$email_address'"; $db->query($sql); if ( !$db->isError() ) { $subject = "Your password at www.jackgodfrey.org.uk"; $message = "Hi, we have reset your password. Username: $username New Password: $random_password http://www.jackgodfrey.org.uk/login.php Once logged in you can change your password Thanks! jackgodfrey.org.uk webmaster This is an automated response, please do not reply!"; // If the email was successfully sent, congratulate the user if (mail($email_address, $subject, $message, "From: jackgodfrey.org.uk Webmaster<[email protected]>\n X-Mailer: PHP/" . phpversion())) { return 'Correct'; } else { // Otherwise give them an error message return 'Something went wrong when the server tried to send your new password. This is usually due to a server error, and is probably not your fault. We apologise for any inconvenience caused.'; } } else { return 'Something went wrong when the server tried to send your new password. This is usually due to a server error, and is probably not your fault. We apologise for any inconvenience caused.'; } } } and all the errors i get listed out in the apache error log [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: email in d:\\Apache\\htdocs\\copy of working\\forgot-password.php on line 49 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: host in d:\\Apache\\htdocs\\copy of working\\php\\functions.php on line 406 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: dbUser in d:\\Apache\\htdocs\\copy of working\\php\\functions.php on line 406 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: dbPass in d:\\Apache\\htdocs\\copy of working\\php\\functions.php on line 406 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: dbName in d:\\Apache\\htdocs\\copy of working\\php\\functions.php on line 406 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Could not connect to server in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 80 [Fri Aug 22 22:16:38 2008] [error] PHP Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 111 [Fri Aug 22 22:16:38 2008] [error] PHP Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 112 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Query failed: SQL: SELECT username, email FROM users WHERE email='' LIMIT 1 in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 113 [Fri Aug 22 22:16:38 2008] [error] PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 171 any help greatly appreciated Link to comment https://forums.phpfreaks.com/topic/120925-solved-nightmares-with-my-database-class/ Share on other sites More sharing options...
corbin Posted August 22, 2008 Share Posted August 22, 2008 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: email in d:\\Apache\\htdocs\\copy of working\\forgot-password.php on line 49 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: host in d:\\Apache\\htdocs\\copy of working\\php\\functions.php on line 406 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: dbUser in d:\\Apache\\htdocs\\copy of working\\php\\functions.php on line 406 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: dbPass in d:\\Apache\\htdocs\\copy of working\\php\\functions.php on line 406 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Undefined variable: dbName in d:\\Apache\\htdocs\\copy of working\\php\\functions.php on line 406 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Could not connect to server in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 80 [Fri Aug 22 22:16:38 2008] [error] PHP Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 111 [Fri Aug 22 22:16:38 2008] [error] PHP Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 112 [Fri Aug 22 22:16:38 2008] [error] PHP Notice: Query failed: SQL: SELECT username, email FROM users WHERE email='' LIMIT 1 in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 113 [Fri Aug 22 22:16:38 2008] [error] PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\\Apache\\htdocs\\copy of working\\php\\database\\MySQL.php on line 171 Let's dissect your errors. Lots of undefined variables. MySQL couldn't connect. Couldn't run a query. Couldn't check number of rows on a failed query. Now let's think about why MySQL can't connect. Remember those undefined variables? Those are the variables that you're passing to MySQL. It would be the equivalent of trying to do: $l = mysql_connect(null, null, null); Google "variable scope in PHP" if you don't understand why this is happening. Link to comment https://forums.phpfreaks.com/topic/120925-solved-nightmares-with-my-database-class/#findComment-623345 Share on other sites More sharing options...
AdRock Posted August 22, 2008 Author Share Posted August 22, 2008 I understand that but if you are including a file and creating a new object within that function, wouldn't the variables be inb the scope of that function? Link to comment https://forums.phpfreaks.com/topic/120925-solved-nightmares-with-my-database-class/#findComment-623355 Share on other sites More sharing options...
trq Posted August 23, 2008 Share Posted August 23, 2008 Where are the $host, $dbUser, $dbPass and $dbName variables defined? Link to comment https://forums.phpfreaks.com/topic/120925-solved-nightmares-with-my-database-class/#findComment-623684 Share on other sites More sharing options...
AdRock Posted August 23, 2008 Author Share Posted August 23, 2008 I include a file called connection.php which has all those variables defined function forgot_password($email) { require_once("php/database/connection.php"); Link to comment https://forums.phpfreaks.com/topic/120925-solved-nightmares-with-my-database-class/#findComment-623690 Share on other sites More sharing options...
corbin Posted August 23, 2008 Share Posted August 23, 2008 require_once does exactly what it says, it requires a file... once. Maybe you've already required it somewhere else, which would make that require_once statement entirely useless. Link to comment https://forums.phpfreaks.com/topic/120925-solved-nightmares-with-my-database-class/#findComment-623839 Share on other sites More sharing options...
Naez Posted August 23, 2008 Share Posted August 23, 2008 I understand that but if you are including a file and creating a new object within that function, wouldn't the variables be inb the scope of that function? I do not believe so. In my opinion you should create your database connection before invoking the function, then send the $db object to the function. Link to comment https://forums.phpfreaks.com/topic/120925-solved-nightmares-with-my-database-class/#findComment-623840 Share on other sites More sharing options...
AdRock Posted August 23, 2008 Author Share Posted August 23, 2008 I have got it working....I had to set the variables as global variables inside the function Link to comment https://forums.phpfreaks.com/topic/120925-solved-nightmares-with-my-database-class/#findComment-623843 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.