Lightme Posted October 24, 2014 Share Posted October 24, 2014 On the mainpage I am getting this error: Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\page\controller\function.php on line 393 $result = $db->query($query); And on the admin section: Notice: Undefined index: role in C:\xampp\htdocs\page\admin\header.php on line 7Notice: Undefined index: role in C:\xampp\htdocs\page\admin\header.php on line 7Fatal error: Undefined class constant 'site_url' in C:\xampp\htdocs\page\admin\header.php on line 8 if ($_SESSION['role'] !== 'admin' and $_SESSION['role'] !== 'moderator'){header( 'Location:'.config::site_url.'index.php' ); Quote Link to comment https://forums.phpfreaks.com/topic/292042-old-code-mysql-to-mysqli/ Share on other sites More sharing options...
Psycho Posted October 24, 2014 Share Posted October 24, 2014 1. Where are you defining $db to be an object prior to that call to the query() method? 2. Where is $_SESSION['role'] defined? And have you included the class "config" and does it have a constant called site_url defined? The first two errors for that are just "Notice" errors and should be suppressed in a production environment, but the last one is a fatal error. That you definitely need to resolve. You shoudl also put an exit() after the header redirect. Quote Link to comment https://forums.phpfreaks.com/topic/292042-old-code-mysql-to-mysqli/#findComment-1494637 Share on other sites More sharing options...
Lightme Posted October 24, 2014 Author Share Posted October 24, 2014 First off all thanks for the awnser I am still in a learning curve. 1. Hope I got this right but it is in the config.php file line 55: $db = new mysqli($config['db_host'], $config['db_user'], $config['db_password'], $config['db_name']); 2. This question I am not complete getting. But I give it a try. (function.php): $urls = isset($_SESSION['user_id']) ?detail($row):'./signup'; Quote Link to comment https://forums.phpfreaks.com/topic/292042-old-code-mysql-to-mysqli/#findComment-1494643 Share on other sites More sharing options...
Psycho Posted October 25, 2014 Share Posted October 25, 2014 #1, how do you know that creating the new $db object is not failing? Do you have any error handling? The error you are getting would seem to indicate that $db is not getting created correctly. Try doing this right after line 55 to see if $db is an object or not. If it is FALSE, then it is failing somehow. var_dump($db); #2, That line does not show that $_SESSION['role'] is being defined. This error Notice: Undefined index: role in C:\xampp\htdocs\page\admin\header.php on line 7 indicates that you are trying to reference that value but it is not defined. This error Fatal error: Undefined class constant 'site_url' in C:\xampp\htdocs\page\admin\header.php on line 8 indicates that there is no "site_url" defined in the class "config" which you are referencing like this: config::site_url Quote Link to comment https://forums.phpfreaks.com/topic/292042-old-code-mysql-to-mysqli/#findComment-1494646 Share on other sites More sharing options...
Lightme Posted October 25, 2014 Author Share Posted October 25, 2014 (edited) 1. This I have for error handling of the database: config.php *line 57 if(!$db){ die('Unable to connect to database [' . $db->connect_error . ']'); } 2, Hope I get this I am really trying hard to understand this line, (really need to study more PHP, really apriciate let me think about the script. header.php if ($_SESSION['role'] !== 'admin' and $_SESSION['role'] !== 'moderator'){ header( 'Location:'.config::site_url.'index.php' ); Edited October 25, 2014 by Lightme Quote Link to comment https://forums.phpfreaks.com/topic/292042-old-code-mysql-to-mysqli/#findComment-1494651 Share on other sites More sharing options...
Lightme Posted October 25, 2014 Author Share Posted October 25, 2014 When I put in on a production enviorment I don't get any errors, by the way. Guess I will then work on it live... strange... I still want to learn the errors... Quote Link to comment https://forums.phpfreaks.com/topic/292042-old-code-mysql-to-mysqli/#findComment-1494656 Share on other sites More sharing options...
maxxd Posted October 25, 2014 Share Posted October 25, 2014 Just because you're not getting the errors doesn't mean they're not happening - production servers typically do and should suppress error display - the user doesn't need to know that much about your server set-up. Granted, you should be seeing your fatal error. Have you used session_start() to actually start the browser session and have you defined the constant $site_url in the config class before attempting to use either $_SESSION['role'] or config::site_url? Quote Link to comment https://forums.phpfreaks.com/topic/292042-old-code-mysql-to-mysqli/#findComment-1494707 Share on other sites More sharing options...
mac_gyver Posted October 25, 2014 Share Posted October 25, 2014 when you use OOP mysqli syntax, the connection (new mysqli(...)) will always return an object. you must specifically test for connection errors using one of the two methods shown in the php.net documentation - /* * This is the "official" OO way to do it, * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0. */ if ($mysqli->connect_error) { // your error handling code here... } /* * Use this instead of $connect_error if you need to ensure * compatibility with PHP versions prior to 5.2.9 and 5.3.0. */ if (mysqli_connect_error()) { // your error handling code here.... } had you used the procedural mysqli_connect() syntax, your testing of the $db value would have worked. i recommend skipping mysqli and use PDO as it has far fewer consistency problems like this, especially if you are going to use prepared queries. Quote Link to comment https://forums.phpfreaks.com/topic/292042-old-code-mysql-to-mysqli/#findComment-1494714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.