jeppers Posted February 13, 2011 Share Posted February 13, 2011 i am really struggling and i have no idea what to do. Call to a member function stmt_init() on a non-object on this code. if (isset($_POST['insert'])) { require_once('includes/connection.php'); // initialize flag $OK = false; // create database connection $conn = dbConnect('write'); // initialize prepared statement $stmt = $conn->stmt_init(); // create SQL i am using a book but i have tried it many ways and the same error occurs. i have looked on forums and they say that i have not set up the users correctley. but for a test i give my user all the permitions that i could and same problem. the error occurs on this line $stmt = $conn->stmt_init(); with the error message stating Call to a member function stmt_init() on a non-object please if you have seen this before could you help thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/227518-call-to-a-member-function-stmt_init-on-a-non-object/ Share on other sites More sharing options...
trq Posted February 13, 2011 Share Posted February 13, 2011 Obviously $conn is not an object. What does..... var_dump($conn); produce? Quote Link to comment https://forums.phpfreaks.com/topic/227518-call-to-a-member-function-stmt_init-on-a-non-object/#findComment-1173550 Share on other sites More sharing options...
jeppers Posted February 13, 2011 Author Share Posted February 13, 2011 it holds bool(true) Quote Link to comment https://forums.phpfreaks.com/topic/227518-call-to-a-member-function-stmt_init-on-a-non-object/#findComment-1173554 Share on other sites More sharing options...
trq Posted February 13, 2011 Share Posted February 13, 2011 There is your problem then. can we see your dbConnect() function? Quote Link to comment https://forums.phpfreaks.com/topic/227518-call-to-a-member-function-stmt_init-on-a-non-object/#findComment-1173838 Share on other sites More sharing options...
jeppers Posted February 16, 2011 Author Share Posted February 16, 2011 <?php function dbConnect($usertype, $connectionType = 'mysqli') { $host = 'localhost'; $db = 'gallery'; if ($usertype == 'read') { $user = '****'; $pwd = '*****'; } elseif ($usertype == 'write') { $user = '*****'; $pwd = '******'; } else { exit('Unrecognized connection type'); } if ($connectionType == 'mysqli') { return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database'); } } ?> there it is i just don't no what i am doing wrong. i have tried so may things please help Quote Link to comment https://forums.phpfreaks.com/topic/227518-call-to-a-member-function-stmt_init-on-a-non-object/#findComment-1175187 Share on other sites More sharing options...
mallard Posted March 8, 2011 Share Posted March 8, 2011 I just found this thread from a search. I'm using the same book as the OP and I'm having the same exact problem. Using the same exact code. Does anybody know what the fix for this might be? If $conn returns boolean true is that why we get the error "Fatal error: Call to a member function stmt_init() on a non-object"? What type of result should $conn return so that this error won't get thrown when we use the stmt_init() function? Thanks for any follow up! Quote Link to comment https://forums.phpfreaks.com/topic/227518-call-to-a-member-function-stmt_init-on-a-non-object/#findComment-1184401 Share on other sites More sharing options...
PFMaBiSmAd Posted March 8, 2011 Share Posted March 8, 2011 The problem is that putting or die() on the end of a new mysqli() statement won't ever die because a new mysqli() statement will always return an object even if the connection fails. You would test if an OOP mysqli connection worked or failed by using mysqli_connect_error() or by using mysqli->connect_error in those versions of php where they fixed the mysqli code to work. Having error_reporting set to E_ALL (or to a -1) would be producing a warning message at the failing new mysqli() statement. Also, by putting the or die() on the end of the new mysqli() statement, the value returned is always converted to a bool(true), even with a successful connection, and the or die() should be removed, even if you don't change the code to use mysqli_connect_error(). Quote Link to comment https://forums.phpfreaks.com/topic/227518-call-to-a-member-function-stmt_init-on-a-non-object/#findComment-1184447 Share on other sites More sharing options...
mallard Posted March 9, 2011 Share Posted March 9, 2011 Very cool. Thanks for your help, PFMaBiSmAd. And thanks for clearly explaining this. Hopefully the OP will come back and see this. Your solution works. I can successfully register a user and add them to a database. So, here's what I did. I changed the last IF statement in the dbConnect function to this: if ($connectionType == 'mysqli') { return new mysqli($host, $user, $pwd, $db); } elseif ($mysqli->connect_error) { die('Connect Error: ' . $mysqli->connect_error); } Quote Link to comment https://forums.phpfreaks.com/topic/227518-call-to-a-member-function-stmt_init-on-a-non-object/#findComment-1184893 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.